Discussion Locked

visual basic and batch files

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

how to call a dos batch file from visual basic code.explain in detail,this will help
thanks in advance.

5 replies

by joe giaquinto

These examples were extracted from applications I've written in the past. However, you will need to adapt, experiment and test them for your own use.
Good luck :)

Examples are listed below:

Notes about the examples:
1. You can run a PIF file(a DOS shortcut) that you've previously created.
2. You can run an executable or batch (bat) file directly.
3. You can pass a string name (such as the path and filename to run).
4. You can pass parameters (if the target application accepts them).
5. You can put long filenames in quotes. You may have to use Chr(34), the quote character.
6. There are a few PIF directives for DOS batch files listed here: k=keep open, c=close after done, etc.
7. The first example uses the DOS command.pif interpreter (the default interpreter).
8. You may have timing issues. Remember that the DOS program will run asynchronously from your application. Depending on what you are running, it may finish before or after your VB processes complete. Therefore youmay have to check for the active DOS program window/window caption or the Win-OS ROT table to see if your application is still out there. You may have to install a do-loop to wait for it.
9. Also check out the use of the Run ! command.
10. Finally, VBA has some examples of Shell.

Dim bat As Long
Dim strRunThisBatchFile As String
strRunThisBatchFile = Chr(34)+"C:\MyPath\MyPIFName.PIF"+Chr(34)+" "+parm1+" "+parm2+" "+parm3
bat=Shell("C:\Command.Com /c"+strRunThisBatchFile)
bat=Shell(strRunThisBatchFile)
bat=Shell("C:\MyPath\MyProgram.Exe")
bat=Shell("C:\MyPath\MyBatchFile.Bat")

by peyison

To call a batch file, or any external executable, you can use the shell command. For a batch file, the call would look like:

Shell Environ("Comspec") & " /c C:\test.bat"

Environ("Comspec") returns Command.com for Win9X and cmd.com for NT.

If you use Shell with just the name of the batch file - like:

Shell "C:\test.bat"

The batch file will run, but the DOS window will remain open.

Your code will keep executing - it will not wait for the batch file to finish. If you would like it to wait, you can use this routine:

Public Sub ShellAndWait(asCommand As String, Optional asCommandLine As String = "")
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long

lPid = Shell(asCommand & " " & asCommandLine)
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, INFINITE)
CloseHandle (lHnd)
End If
End If

End Sub

In the Declarations section of the code module, add:

PublicDeclare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFF

The call to the routine would look like:

ShellAndWait Environ("Comspec")," /c C:\test.bat"

The code that follows this line will not run until the batch file is finished.

Hope this helps.

This discussion is closed to new replies.