codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Run a .bat file from VB  Danny_Sales at 12:19 on Thursday, April 22, 2004
 

I want to run a .bat file from VB, any ideas ?

  Re: Run a .bat file from VB  aco_pa at 06:44 on Tuesday, May 11, 2004
 

I was wondering just that...I have looked for a long time for it and found nothing.

  Re: Run a .bat file from VB  jermcode at 17:06 on Sunday, May 16, 2004
 

There's probably a better way to do what you're trying to do, but this will probably work:

dim shell as new wscript.shell
shell.run "batchfile.bat"



  Re: Run a .bat file from VB  dip_schitt at 06:03 on Monday, May 17, 2004
 

Well, I tried to use that code and it alone is not enough to open a batch file. Maybe you can explain more.

Thanks!


  Re: Run a .bat file from VB  jermcode at 06:26 on Monday, May 17, 2004
 

That's all I've got.

If you're using vbscript, you can do this:

dim shell
set shell=createobject("wscript.shell")
shell.run "batchfile.bat"
set shell=nothing


so you should be able to call the same automation object in VB.

for VB.NET this will work:

using System;

namespace Learn
{
class cmdShell
{
[STAThread]
static void Main(string[] args)
{
Declare New Process
System.Diagnostics.Process proc;

Run test.bat from command line.

proc = System.Diagnostics.Process.Start("C:\\test.bat");

Waits for the process to end.

proc.WaitForExit();
}
}
}



in VB you might be able to import the shell exec api:

Private Declare Function ShellExecute _
Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

you can then use shellexecute to run "mybat.bat"

see this page for more info:

http://custom.programming-in.net/articles/art9-1.asp?f=ShellExecute


  Re: Run a .bat file from VB  Drew G. Bowman at 20:05 on Monday, May 17, 2004
 

In Javascript on WINDOWS you can use this to open aplications:




<object ID="AppCtl" WIDTH="0" HEIGHT="0"
CLASSID="CLSID:07340190-BCE0-11D1-BD7E-00207812DE95">
</object>

<a href="javascript:AppCtl.RunApplication('C:\\program%20files\\internet%20explorer\\iexplore.exe');">Internet Explorer</a>

<! use "\\" as a "\" in the address ==========>

  Re: Run a .bat file from VB  jermcode at 00:13 on Tuesday, May 18, 2004
 

The question was how to run a bat file from VB- that's why I included VB/VBS examples, and not j/html examples...

  Re: Run a .bat file from VB  subhashsubramanyam at 17:16 on Tuesday, April 03, 2007
 

Would you also please add the lines of code how we can pass some arguments along with the batch file on the command line to execute

something like "\\Server\Job.bat" & "Successfully Executed"

  Re: Run a .bat file from VB  Smudge202 at 13:22 on Thursday, July 03, 2008
 

If you want a message box to display if the execution of the batch file suceeded then you need to do a couple things.

The easiest way I believe is add a net send or echo command to the end of the .bat you are running.

http://www.robvanderwoude.com/usermessages.html

The above link will show you how to add message box type commands to the batch script.

If you use the net send command, make sure the messenger service is enabled and started else it will fail.

Good luck,

Smudge

  Re: Run a .bat file from VB  BeauregardRippey at 19:17 on Wednesday, February 18, 2009
 

This works with VB6:

Dim RetVal
RetVal = Shell("(path)\(file).bat", vbWindowState)

However, any code after the shell function call might run even if the batch file hasn't finished its task.
To get around this, you could do several different things.
You could put code in your batch file to create a "/forum/done.txt" file in some designated folder, and in the code following the shell call, enable a timer control. Within the timer event, put code to check for the existence of the "/forum/done.txt" file, and then delete the "/forum/done.txt" file from VB and resume normal execution of the VB code (I like using the scripting.filesystemobject to do this-- you need to have "Microsoft Scripting Runtime" in your VB project references).

  Re: Run a .bat file from VB  BeauregardRippey at 20:24 on Thursday, February 19, 2009
 

To make the batch file run in a synchronous manner (i.e.-- VB6 code doesn't continue executing until the batch file is finished running), there's a much simpler method than I mentioned previously.
In project references, make sure "Windows Script Host Object Model" is selected, and use the following code:

Dim wshThisShell As WshShell
Dim lngRet as Long
Dim strShellCommand As String
Dim strBatchPath as String

Set wshThisShell = New WshShell
strBatchPath = "C:\Program Files\Batch Path\Batch.bat"
'the path for the batch file you're using
strShellCommand = """" & strBatchPath & """"
'the ridiculous number of quotation marks is necessary
'when there is a space in one or more of the folder names
lngRet = wshThisShell.Run(strShellCommand, vbNormalFocus, vbTrue)
'set 3rd argument above to vbFalse for asynchronous
'execution of the batch file.

  Re: Run a .bat file from VB  alexdenipaul at 10:31 on Monday, October 12, 2009
 

Whenever we are executing vbscripts using Altiris, it is a best practice to return Exit Codes/Return Codes to the Altiris. Following is the template that not only return an Exit code but also writes to the Event Log consolidate debt.

Note: Place the executable and script in one place

'--------------------------------------------------
'GET CURRENT FOLDER PATH
path = FileSystemObject.GetParentFolderName(WScript.ScriptFullName)
If Right(path, 1) <> "\" Then path = path & "\"
End If

errTrap = shell.Run(path, 1 ,True)

If (errTrap = 0) Or (errTrap = 3010) Then
'WRITE EXIT CODE [0-success/3010-success&requires reboot] TO EVENTLOG
shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap
WScript.Quit (errTrap)
payday loans
Else

'WRITE EXIT CODE TO EVENTLOG

shell.LogEvent vbLogError, sLogHeader & "Installation failed." & VbCrLf & "Exit code: " & errTrap
WScript.Quit (errTrap)

End If


Set FileSystemObject = Nothing
Set Shell = Nothing
'--------------------------------------------------


personal loans


One needs to define sLogHeader variable so that the hedder of the event log will written with this name, befor the following line in the above script
(shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap).

Example
sLogHeader = "Install batch file"

Preferably sLogHeader name should be task name that you are running so that it will be essy identify in event log foreclosures.

I hope this may help.

Thanks









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: how to open instantly a pop up window whenever a user sends a message to the other user
•  Re: PING PROGRAM IN PERL
•  Re: date of birth validation using java script-very urgent
•  JAVA If statements
•  Need Help with a Regular Expression
•  Informations on HTML
•  Re: FREE TV on your PC!
•  Re: Using Lucene or Solr ?
•  submit button on html form


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010