Discussion Locked

Batch file in VB

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I want to execute a batch file from vb.next line shld execute only after completing the execution of batch file.Can somebody help me

5 replies

by Member 25348569

You use the Shell object to run the batch file with the bWaitOnReturn value set to True. The next line won't execute until the batch file exits.

This vbscript snippet will give you the idea.

set oshell = CreateObject("Wscript.Shell")
oShell.run "Batchfilepath",0,True
more lines

by scott maclean

The following code will not only do what you ask, it will allow you to set a timeout should you wish it to wait only up to a certain amount of time (to avoid locking up your app should the batch file not return):

Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Const SW_SHOWNORMAL = 1
Private Const INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

' Shell out and suspend operation until the shelled process is complete
' Parameters are the Command and timeout (in milliseconds). If timeout is
' zero, then wait is infinite

Function Exec(Cmd As String, Optional Hide As Boolean, Optional TimeOut As Long, Optional ReturnState As Integer, Optional NoWait As Boolean) As Long

Dim iTask As Long, ret As Long, pHandle As Long, WaitTime As Long

WaitTime = TimeOut
If TimeOut = 0 Then WaitTime = INFINITE

If Hide Then
iTask = Shell(Cmd, vbHide)
Else
iTask = Shell(Cmd, vbNormalFocus)
End If

If NoWait <> True Then
Exec = iTask
pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
ReturnState = WaitForSingleObject(pHandle, WaitTime)
If ReturnState <> 0 Then Shell "kill " & Format$(iTask), vbHide
ret = CloseHandle(pHandle)
End If

End Function

This discussion is closed to new replies.