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 DOS Scripts from VB 2

Status
Not open for further replies.

LegoPiece

Programmer
Mar 19, 2001
95
US
Is it possible to call command line statements from within Visual Basic, and perhaps take it a step further + run batch scripts?

Peace Thanks!
LeGo PiEcE

"The Computer Spock! Destroy it!" - Captain James T. Kirk
 
sure,
create a batch file (foo.bat)
and use the SHELL command
varResult = Shell("foo.bat")

The Shell command will pick the "right" process on the basis of extension (from same table as Explorer uses) and thus you can use this to launch Word, Excel, ....

The Shell command DOES NOT guarentee that the application so launched is done when it returns....to handle this you need dig a little deeper.

DsB

 
Right on DsB! God, and I've been using the Shell command for launching applications for ages too... talk about right under your nose! Thanks!
LeGo PiEcE

"The Computer Spock! Destroy it!" - Captain James T. Kirk
 
Using the shell command is the way to go. It took me a couple of days of banging my head off of my desk to think of this. To make VB wait until the shell command is finished try this:

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

iTask = Shell(batch_file_name_here, vbNormalFocus)
pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
ret = WaitForSingleObject(pHandle, INFINITE)
ret = CloseHandle(pHandle)

It worked for me. Just remember to set the property of the DOS window to close on exit from the batch file otherwise the window will stay open and in the middle of your screen until you close it manually.
 
"please grant the answer a star, kind sir:"
(grovel-grovel, beg-beg)
 
Grovel and beg no more.

In my typical dunder-headed way, I left out something you will need to make VB wait for the shell command to finish.

Add this in (and forgive my stupidity):

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 INFINITE = -1&
Private Const SYNCHRONIZE = &H100000
 
LOL - Stars Abound! Thanks!
LeGo PiEcE

"The Computer Spock! Destroy it!" - Captain James T. Kirk
 
another star to mohrorless (Programmer) for his for his elegant waiting game.
 
ok, for your wait-on-the-shell-started-process-to-finish solution....which provided a lot of useful api's and use thereof. guess calling this a waiting-game was too glib.

sorry...it was a complement
 
No insult taken. I was hip deep ([machinegun]) in a bug on my project and my mind was having trouble figuring our what you meant.


I thank you for the compliment.........

[spidey]
 
There's another way of doing this, if you have the Windows Script Host installed on your machine (which you will if you are running 98 or W2K). Set a reference to the Windows script Host Object Model and create a new instance of the WSHShell class the code:

Dim objShell As New WshShell
objShell.Run "notepad.exe", , True [\b]
will shell an instance of notepad and [\b] wait until it has finished running. The missing second parameter allow you to specify a window style: maximised, minimized, hidden etc. Very easy, and not an API call in sight! :)
 
dcdavies..

I tried your code and it gave me an error.
user defined type not defined.

vb6 dose not know what a New WshShell is.

can you please help me

elwood
 
Under project add a reference to Windows Script Host Obect Model Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top