Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How exactly using VB6 can I can call any Windows shell command as you would from the command-line?

For example, something as trivial as:

echo foo

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 wshShell
Dim lngReturnCode As Long
Dim strShellCommand As String
Set shell = New wshShell
strShellCommand = "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.

This code is for vbScript not VB6 as asked in the question. That can be very confusing for someone that only knows VB6 and looking for answers to VB6 questions. – Jeff Nov 2, 2015 at 4:12

you should think in expanding COMSPEC environment variable if you wish to support earlier systems like windows 9x or me.

You can also obtain the process id using

pid = Shell(Environ("COMSPEC") & " /c echo foo", vbNormalFocus)

Example - send confirmation pass to make a task:

shell (""echo pass|schtasks /create /TR "C:\folder\...\program.exe" /more_parameters"")

because the first " are closed in "C:\... and the string would stop.

This won't pass a syntax check, nor is the style correct. " starts and ends a string. "" inside a string adds a quote symbol. "" outside a string represents a zero-length string. Also, in VB6, you do not put sub parameters in parentheses. So this line will not compile. – TomXP411 Aug 12, 2021 at 20:33

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.