Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running batch file from VB

Status
Not open for further replies.

Jacob438

Programmer
Jul 27, 2001
69
US
I am having one heck of a time trying to run a batch file from VB. I really need help ASAP for my project. Basically what I am trying to do is open a batch file, run it, then open another batch file and run it. I want to wait till one batch file finished before beginning the second. However, I am having problems getting VB to even run the batch file. I am using the shell command, but all that happens is that it opens a DOS prompt window for a split second, then closes it. I know the batch file isn't being run because it takes 20 minutes to run. I am using Win2k workstation and I really need help. Let me know if you need more information.

Thank you,

Jacob
 
I have run .bat files from VB before with not too much problem, want to give me more info and I will see if I can help you? The hardest part I had was passing parameters through DOS but once I figured that out it seemed to run pretty straight forward, of course I never wrote one that ran 20 mins either *grin* :)

Joanne
 
I've had the same problem in the past with Windows 2000; try the exact same program in Win9x and you should have no problem. I still haven't anything (not even in MS TechNET) that mentions it though. I have heard some people say they have gotten it to work by using the ShellExecute API function; I included the basic syntax of it below. I haven't gotten a chance to try it myself yet so if it works (or if you find a solution) please let me know.

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
 
Well,

I've found out something, but it is still strange. My application WILL run fine on Win2k if the application is located on the C:, however, when it's on my E:, it will not exectue the batch file. I'm pretty sure I've got the path correctly because I used App.Path & file.bat. So I don't know what's going on, but if there is a way so that I can run this on the E drive, please let me know. Does it have to do with system files being located on C?

Any help will be appreciated, thank you,

Jacob
 
This is nice code guy´s and it works

I am from Denmark and whan´t to go U.S.A just need a job !

Private Sub Form_Load()

Dim WshShell As Object
Set WshShell = CreateObject("Wscript.Shell")

WshShell.run "C:\windows\desktop\bat1.bat", , True
WshShell.run "C:\windows\desktop\bat2.bat", , True

End Sub

Jens
til@infojens.dk
 
It sounds like VB is indeed launching your batch by the existance of the DOS window, VB couldnt launch DOS because you're telling it to launch a .bat file, which then opens the command processor because the OS has .bat associated with the command processor of course.

It could be something to do with your PATH statement, since you're having problems when the .bat file is on your E: drive, it sounds like something the .bat file needs is unavailable to it when launched from the E: drive. Can you run the .bat file yourself with success while it is on the E: drive?

Also, I think I understood that you want the batch to launch, finish then have your script launch another. The Shell command alone won't usually do that, it basically does a "start" command and as soon as the application starts, VB returns the process ID and continues to the next line of code. (look in the FAQ's for more on that)

You can use that return ID to check to see that VB is indeed launching your batch file, the process ID will be that of cmd.exe or command.com, whichever it launches...
 
Just a few thoughts . . .

1)When the Command Console opens up for the split second that you mentioned, do you get an error message of any type? If it is too fast to see, then modify your Shell command to pipe the output to a text file . . . something like "E:\MyBatFile.Bat>MyOutputFile.txt" This will place any messages (including possible errors) that are produced into the text file. That might give you some more information and some more insight into what is happening.

2) Also, you will probably what to use the WaitForSingleObject API to force you application to pause and wait for the externally shelled process to run.

3) One final thought . . . can you rewrite the Bat file in VB? Perhaps in a DLL so the logic could be reused in any of your applications. It would be easier to integrate and it would probably run faster than 20 minutes. - Jeff Marler B-)
 
Private Sub Command2_Click()
' replace c:\windows\arp.exe with the file you want to run.
' the "/k" parameter tell the DOS environment to run the file and not close
' the DOS window after the file finish running.
' "vbMaximizedFocus" is the state of the DOS window. that's mean the
' DOS window will be maximized.
' instead of "vbMaximizedFocus" you can use the following parameters:
' vbMinimizedFocus, vbMinimizedNoFocus, vbNormalFocus, vbNormalNOFocus, vbHide
Shell ("command.com /k c:\windows\arp.exe"), vbMaximizedFocus
End Sub

Private Sub Command3_Click()
' the "/c" parameter tell the DOS environment to run the file and close
' the DOS window after the file finish running.
Shell ("command.com /c c:\windows\arp.exe"), vbMaximizedFocus
End Sub

Private Sub Command4_Click()
' vbHide parameter will cause the DOS environment to run in invisible mode.
Shell ("command.com /c c:\windows\arp.exe"), vbHide
End Sub

Eric De Decker
vbg.be@vbgroup.nl

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top