How do I call a Windows shell command using VB6? How do I call a Windows shell command using VB6? windows windows

How do I call a Windows shell command using VB6?


Here's how you do it :

Shell "cmd echo foo", vbNormalFocus 


I've always used the Run method of the wshShell object, which is available after you reference the Windows Script Host Object Model in your project:

Dim shell As wshShellDim lngReturnCode As LongDim strShellCommand As StringSet shell = New wshShellstrShellCommand = "C:\Program Files\My Company\MyProg.exe " & _   "-Ffoption -Ggoption"lngReturnCode = shell.Run(strShellCommand, vbNormalFocus, vbTrue)

You get the same functionality as the normal Shell statement, but the final parameter lets you decide whether to run the shelled program synchronously. The above call, with vbTrue, is synchronous. Using vbFalse starts the program asynchronously.

And, as noted in previous answers, you need to run the command shell with the "/c" switch to execute internal commands, like the "echo foo" from your question. You'd send "cmd /c echo foo" to the Run method.


Shell and ShellExecute?

http://msdn.microsoft.com/en-us/library/aa242087.aspx

Dim RetValRetVal = Shell("C:\WINDOWS\CALC.EXE", 1)   ' Run Calculator.