a_padwal
asked on
Executing .bat file through vb
I want to execute .bat file on button click.Presently I am using shell function.But before complete execution of .bat file it is exiting from that file.So full execution is not happenning.
my code is
RetVal = Shell("C:\BlueGenet\test.b at", 1)
MsgBox RetVal
before complete execution of .bat file it is showing msgbox with number and exits from that file.
any suggetion.
my code is
RetVal = Shell("C:\BlueGenet\test.b
MsgBox RetVal
before complete execution of .bat file it is showing msgbox with number and exits from that file.
any suggetion.
How about this..
' AppActivate can also use the return value of the Shell function.
MyAppID = Shell("C:\WORD\WINWORD.EXE ", 1) ' Run Microsoft Word.
AppActivate MyAppID, true ' Activate Microsoft ' Word.
' AppActivate can also use the return value of the Shell function.
MyAppID = Shell("C:\WORD\WINWORD.EXE
AppActivate MyAppID, true ' Activate Microsoft ' Word.
in shell function you call an exe or a batch file and then proceed to next line...
us this code. it is using a function shellandwait
========================== ========== ========== =======Opt ion Explicit
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400
Public Function Shellandwait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean
On Error GoTo ErrorHandler
lStart = CLng(Timer)
sExeName = ExeFullPath
'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If
lInst = Shell(sExeName, vbMinimizedNoFocus)
lProcessId = OpenProcess(PROCESS_QUERY_ INFORMATIO N, False, lInst)
Do
Call GetExitCodeProcess(lProces sId, lExitCode)
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode = STATUS_PENDING
Shellandwait = True
Exit Function
ErrorHandler:
Shellandwait = False
Exit Function
End Function
us this code. it is using a function shellandwait
==========================
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400
Public Function Shellandwait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean
On Error GoTo ErrorHandler
lStart = CLng(Timer)
sExeName = ExeFullPath
'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If
lInst = Shell(sExeName, vbMinimizedNoFocus)
lProcessId = OpenProcess(PROCESS_QUERY_
Do
Call GetExitCodeProcess(lProces
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode = STATUS_PENDING
Shellandwait = True
Exit Function
ErrorHandler:
Shellandwait = False
Exit Function
End Function
Sorry, u can use Shell function.
Try this..
Private Sub Command1_Click()
MyAppID = Shell("C:\windows\calc.EXE ", 1) ' Run
AppActivate MyAppID, True
MsgBox "Run..."
End Sub
Hope this can help. Thanks first. ^_^
Try this..
Private Sub Command1_Click()
MyAppID = Shell("C:\windows\calc.EXE
AppActivate MyAppID, True
MsgBox "Run..."
End Sub
Hope this can help. Thanks first. ^_^
i am not contradicting yongyih's ans. you can very well do it this way using AppActivate.
but here is a code which is also doing it in different way.
In shell you don't wait for the execution of the exe/bat or the batch file and u immediately move to the next line. if you want to wait till the called exe/bat is executed, u can use the shellandwait function.
just copy paste the code sent above in a module and use
shellandwait function.
hope this gives you another way to solve the problem.
-A. Pandey
but here is a code which is also doing it in different way.
In shell you don't wait for the execution of the exe/bat or the batch file and u immediately move to the next line. if you want to wait till the called exe/bat is executed, u can use the shellandwait function.
just copy paste the code sent above in a module and use
shellandwait function.
hope this gives you another way to solve the problem.
-A. Pandey
ASKER
amitabh
still it is exiting after using using your function.I had given wait of 1000.
yongyih
it is showing invalid App at "AppActivate MyAppID, True"
I am firing two commands from .bat file.
is there any other way to fire commands through vb.
still it is exiting after using using your function.I had given wait of 1000.
yongyih
it is showing invalid App at "AppActivate MyAppID, True"
I am firing two commands from .bat file.
is there any other way to fire commands through vb.
try 0 instead of 1000
and is it possible for u to tell us what is this bat file doing. a bit of code ....i mean
and is it possible for u to tell us what is this bat file doing. a bit of code ....i mean
I agree with Amitabh .
What is this bat file doing what cannot be done directly through the UI?
What is this bat file doing what cannot be done directly through the UI?
Using Api's U can Use shellexecute to execute some exe
then WaitForSingleObject to wait for it to finish
if this is usefull I can send code
(U can also see it in PAQ or www.allapi.net
then WaitForSingleObject to wait for it to finish
if this is usefull I can send code
(U can also see it in PAQ or www.allapi.net
giving u another code which uses waitforsingleobject API as suggested by bahnass
just copy paste it in a *.bas (module) and use the function.
========================== ========== ========== ========
Option Explicit
Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Public Enum enSW 'Private
SW_HIDE = 0
SW_NORMAL = 1
SW_MAXIMIZE = 3
SW_MINIMIZE = 6
End Enum
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Enum enPriority_Class
NORMAL_PRIORITY_CLASS = &H20
IDLE_PRIORITY_CLASS = &H40
HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Function SuperShell(ByVal app As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
'Not used, but needed
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
'Set the structure size
sec1.nLength = Len(sec1)
sec2.nLength = Len(sec2)
sinfo.cb = Len(sinfo)
'Set the flags
sinfo.dwFlags = STARTF_USESHOWWINDOW
'Set the window's startup position
sinfo.wShowWindow = start_size
'Set the priority class
pclass = Priority_Class
'Start the program
If CreateProcess(vbNullString , app, sec1, sec2, False, pclass, _
0&, WorkDir, sinfo, pinfo) Then
'Wait
WaitForSingleObject pinfo.hProcess, INFINITE 'dwMilliseconds
SuperShell = True
Else
SuperShell = False
End If
End Function
========================== ========== ========== ========== ===
just copy paste it in a *.bas (module) and use the function.
==========================
Option Explicit
Const INFINITE = &HFFFF
Const STARTF_USESHOWWINDOW = &H1
Public Enum enSW 'Private
SW_HIDE = 0
SW_NORMAL = 1
SW_MAXIMIZE = 3
SW_MINIMIZE = 6
End Enum
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Enum enPriority_Class
NORMAL_PRIORITY_CLASS = &H20
IDLE_PRIORITY_CLASS = &H40
HIGH_PRIORITY_CLASS = &H80
End Enum
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Function SuperShell(ByVal app As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
Dim pclass As Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
'Not used, but needed
Dim sec1 As SECURITY_ATTRIBUTES
Dim sec2 As SECURITY_ATTRIBUTES
'Set the structure size
sec1.nLength = Len(sec1)
sec2.nLength = Len(sec2)
sinfo.cb = Len(sinfo)
'Set the flags
sinfo.dwFlags = STARTF_USESHOWWINDOW
'Set the window's startup position
sinfo.wShowWindow = start_size
'Set the priority class
pclass = Priority_Class
'Start the program
If CreateProcess(vbNullString
0&, WorkDir, sinfo, pinfo) Then
'Wait
WaitForSingleObject pinfo.hProcess, INFINITE 'dwMilliseconds
SuperShell = True
Else
SuperShell = False
End If
End Function
==========================
to make it more easy for you sending you a function which calls supershell. just paste it too in the module and u can call this function.
========================== ========== ========== ========== =
Public Function synchshell(ByVal app As String, ByVal waittime As wait, ByVal start_size As AP_enSW, ByVal Priority_Class As AP_Priority_Class) As Boolean
Dim p_retshell As Boolean
p_retshell = SuperShell(app, Left$(app, Len(app) - Len(Dir$(app))), waittime, start_size, Priority_Class)
synchshell = p_retshell
End Function
==========================
Public Function synchshell(ByVal app As String, ByVal waittime As wait, ByVal start_size As AP_enSW, ByVal Priority_Class As AP_Priority_Class) As Boolean
Dim p_retshell As Boolean
p_retshell = SuperShell(app, Left$(app, Len(app) - Len(Dir$(app))), waittime, start_size, Priority_Class)
synchshell = p_retshell
End Function
Hi
Dim oShell As Object
Set oShell = CreateObject("WScript.Shel l")
RetVal oShell.Run(("C:\BlueGenet\ test.bat", 1, True)
Set oShell = Nothing
MsgBox RetVal
'RetVal contain return value:
'0 if successfull
'Error code if error occure
'or ERRORLEVEL value from *.bat file.
Cheers
Dim oShell As Object
Set oShell = CreateObject("WScript.Shel
RetVal oShell.Run(("C:\BlueGenet\
Set oShell = Nothing
MsgBox RetVal
'RetVal contain return value:
'0 if successfull
'Error code if error occure
'or ERRORLEVEL value from *.bat file.
Cheers
Now i think we have seen enough ways of calling an exe/bat file synchronously and all are right and working ways. so we need to see where actually you are held up. Also try 0 as the input time as i had written earlier instead of 1000 since 0 means indefinite waiting till the called exe/bat gets executed.
-A.Pandey
-A.Pandey
ASKER
Following are lines in bat file
set PATH=C:\jdk1.3.0_02\bin;%P ATH%
java -cp cl.jar Cl test11 10000 $admin$:0:2:gi:test
"C:\Documents and Settings\abc\Desktop\test\ AutoTest\A utoTest.ex e"
I want to execute .jar and .exe
.jar writes some value to database and after transfering data returns message whether transfermation is succesfull or not.
.exe checks whether all data transfer is done or not.
but before complition of .jar it shifts to .exe.
I have used supershell but it returns False.
set PATH=C:\jdk1.3.0_02\bin;%P
java -cp cl.jar Cl test11 10000 $admin$:0:2:gi:test
"C:\Documents and Settings\abc\Desktop\test\
I want to execute .jar and .exe
.jar writes some value to database and after transfering data returns message whether transfermation is succesfull or not.
.exe checks whether all data transfer is done or not.
but before complition of .jar it shifts to .exe.
I have used supershell but it returns False.
hearing...
i think the execution of jar file not even starts and fails and then your code move to next line.
One possible scenario for which you can check.
you r calling ur exe/bat something like this
shell "c:\program files\my_proj\myexe"
In windows there is no barrier of 8.3 naming convention so our APP.Path becomes something like the above. It takes characters after program as a parameter and assumes program as some exe.
So put your exe/bat in C: drive and try to run the code
shell "c:\myexe"
May be this is the problem i am just guessing. becoz something that can be run from the command prompt shouldn't give any problem from the UI. Plus all the codes sent above like shellandwait and supershell and others are working ones.
cheers!!
One possible scenario for which you can check.
you r calling ur exe/bat something like this
shell "c:\program files\my_proj\myexe"
In windows there is no barrier of 8.3 naming convention so our APP.Path becomes something like the above. It takes characters after program as a parameter and assumes program as some exe.
So put your exe/bat in C: drive and try to run the code
shell "c:\myexe"
May be this is the problem i am just guessing. becoz something that can be run from the command prompt shouldn't give any problem from the UI. Plus all the codes sent above like shellandwait and supershell and others are working ones.
cheers!!
i think you could add a line in your bat but i am not sure.
After calling jar execution add:
@IF ERRORLEVEL THEN PAUSE
set your Shell calling to vbnormal to see what happens, at least to debug.
After calling jar execution add:
@IF ERRORLEVEL THEN PAUSE
set your Shell calling to vbnormal to see what happens, at least to debug.
i don't know if it is applicable here but take a look to this interesting article anyway:
http://www.vbdna.net/articles/comcallingjava.asp
http://www.vbdna.net/articles/comcallingjava.asp
Hi a_padwal,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Refund points and save as a 0-pt PAQ.
*** Possible answer: Use START /WAIT command in the batch file
a_padwal, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit. Explain why.
==========
DanRollins -- EE database cleanup volunteer
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Refund points and save as a 0-pt PAQ.
*** Possible answer: Use START /WAIT command in the batch file
a_padwal, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit. Explain why.
==========
DanRollins -- EE database cleanup volunteer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Note By default, the Shell function runs other programs asynchronously. This means that a program started with Shell might not finish executing before the statements following the Shell function are executed.
So, if u want to finish the bat file first before continue, you have to find another way.
Good Luck.