Click here to Skip to main content
15,883,799 members
Articles / Programming Languages / VBScript
Tip/Trick

Differences between Run and Exec (VBScript)

Rate me:
Please Sign up or sign in to vote.
3.52/5 (7 votes)
12 Mar 2013CPOL2 min read 347.1K   3   11   8
Learn to call external applications in VBScript.

Introduction

When I started programming in VBScript, I didn't know the real difference between Run and Exec in VBScript present in the WScript.Shell object. I'm writing this tip to help you to know when to use Run or Exec and I will show examples that can be implemented in your code (async command-line reading is an interesting thing!).   

Background 

When you need to run an external program that does not have COM access, you need to call CMD. This tip pretends to show you the right choice depending on your objective and program you are calling.  

Run 

We use Run when we are calling an external command-line program (or any command that the prompt recognizes). It can return %ERRORLEVEL% and with this code, we can show a message to the user saying if the operation was success or not. It doesn't support async output, it means that you can't show to the user the output of the external program in real-time, you need to wait till the program exits. It does not support it because Run() reads the exit code that is released only when an external application exits.

VBScript
Set objShell = CreateObject("WScript.Shell")
strErrorCode = objShell.Run ipconfig,0,True
WScript.Echo strErrorCode

With this, ipconfig will be executed in command prompt. The CMD prompt will not be shown (0) and our script will wait till the program exits before it continues processing (True).  

ERRORLEVEL will be set in the strErrorCode variable. 

Exec

We can use it to call GUI applications, because then Run does not call CMD to execute the action.

The principal difference between Exec and Run is that Exec supports real-time output to the user (if it is a command-line program), but we can't get ERRORLEVEL using this method.  

 If we want to call explorer.exe (or an external program), we just need to call it.

VBScript
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("explorer.exe")

For async output, we need to call CMD.exe and read the output with VBScript.

Note: Some programs do not support real-time output (WGet and DISM are examples). Use Run instead.

VBScript
Set objShell = CreateObject("WScript.Shell")
comspec = objShell.ExpandEnvironmentStrings("%comspec%")
 
' ////////////////////////////////////////////////////////

Set objExec = objShell.Exec(comspec & " /c ipconfig")
     Do
          WScript.StdOut.WriteLine(objExec.StdOut.ReadLine())
     LoopWhile Not objExec.Stdout.atEndOfStream
WScript.StdOut.WriteLine(objExec.StdOut.ReadAll)

Image 1

We expand the %comspec% variable (it returns the location of the command-line interpreter of Windows). If you're running Windows NT, it returns CMD.exe. For Windows 9x users, command.com will be called. It will call the correct version of CMD.exe depending on the ARCH of your OS. For example: if you have Windows x64, C:\Windows\system32\cmd.exe (64-bits CMD) will be called, not C:\Windows\SysWow64\cmd.exe (32-bit version). It is important for DISM, for example, that it fails if we try to modify an image if 32-bit CMD is called on an x64-bit environment. 

After calling the command-line, it will read the output in real-time and write changes on the same window of our script. When the program closes, VBScript will read all output from the prompt and write it on screen. 

History 

  • 12 Dec 2012: Tip published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseCode formatting tip Pin
Konstantin Pandipulovich17-Nov-19 23:13
Konstantin Pandipulovich17-Nov-19 23:13 
Questiongracias!!! Pin
Member 1122442126-Jul-17 3:04
Member 1122442126-Jul-17 3:04 
Questionre: Pin
Member 1141270229-Jan-15 8:07
Member 1141270229-Jan-15 8:07 
AnswerRe: re: Pin
Eduardo Mozart de Oliveira16-Oct-16 4:42
Eduardo Mozart de Oliveira16-Oct-16 4:42 
QuestionNeed assistance Pin
Member 110557503-Sep-14 1:52
Member 110557503-Sep-14 1:52 
AnswerRe: Need assistance Pin
Eduardo Mozart de Oliveira16-Oct-16 4:38
Eduardo Mozart de Oliveira16-Oct-16 4:38 
QuestionThank you Pin
Kevin Dondrea16-Jan-14 2:44
Kevin Dondrea16-Jan-14 2:44 
Thanks for explaining the difference. I'm using Windows 7 but I get errors for all the code examples you give. Could it be related to Windows XP vs Windows 7 or the script host you are using?

Thank you,
Kevin
AnswerRe: Thank you Pin
Eduardo Mozart de Oliveira27-Jan-14 9:57
Eduardo Mozart de Oliveira27-Jan-14 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.