Link to home
Start Free TrialLog in
Avatar of grav0005
grav0005

asked on

Retrieving the exit code from a dos.cmd shell process in vbscript

Hey everybody,

My code is actually a vbscript, but I couldn't find it in the languages.  A VB or vbscript solution would be fine with me.

My script launches a dos.cmd file using a Shell and waits for the exit code.  Looks easy enough, but it is always zero!  I tried to set the ERRORLEVEL variable in the dos file, but that didn't work.  Any ideas?  Here is the vbscript code:

Dim wshShell, exitCode
Set wshShell = CreateObject("WScript.Shell")
exitCode = wshShell.Run("dosScript.cmd", 0, WAIT)

'exitCode is always 0!
If exitCode <> 0 Then
  'error!
End If

Thanks,

Rob
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Waits until a  .bat file has completed its processing:

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
    Do
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Private Sub Command1_Click()
    shellwait "C:\myfile.bat", vbhide
End Sub
Avatar of grav0005
grav0005

ASKER

Hi vb_elmar,

Me and my big mouth saying that VB would be OK.  Apparently, you can't call Windows API functions from vbscript, due to potential security issues.  Would you happen to have a non-api solution?

Rob
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wslrfexecmethod.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wslrfscriptexecobject.asp

    Dim WshShell, oExec
    Set WshShell = CreateObject("WScript.Shell")

    Set oExec = WshShell.Exec("calc")

    Do While oExec.Status = 0
         WScript.Sleep 100
    Loop

    WScript.Echo oExec.ExitCode
Hi vb_elmar,

The WshShell.Run solution works like a charm, except that I can't seem to use a variable to exit the dos command script. IE:

set myerr = 2
EXIT %myerr%

does not work while:

EXIT 2 does.(?!?!)

Any ideas?

Rob
Dim WshShell
Dim ReturnCode

Set WshShell = CreateObject("Wscript.shell")
ReturnCode = WshShell.Run("calc.EXE", , True)
30: Set WshShell = Nothing: End


The sample is intended for waiting at line 30
until calc.exe (or a command script) has
completed its processing.

VB waits until calc.exe closes and after this
it continues further operations.
I used the Wait parameter as well:

retVal = WshShell.Run("genExitCode.cmd", 0, true)

The problem ( a dos one at this point ) is that I can't use a variable to exit the genExitCode.cmd script. IE:

THIS CODE IS IN MY "genExitCode.cmd" FILE:

set myerr = 2
EXIT %myerr%

END OF DOS CODE

I would like to return the value of %myerr% to the VBScript.

Thanks,

Rob
Very difficult to do; each time a VBS runs an external program, it creates an 'environment' uses the current environment variables.  When the external program finishes, the new 'environment' is destroyed, taking any changes to the environment varables with it...

In your DOS Script you may want to return the value to a file:

      @echo %myerr% > C:\EXITCODE.DAT

then use the file system object to retrieve it:

      Dim oFSO, oTS, szExitCode
      Set oFSO = CreateObject("Scripting.FileSystemObject")

      If oFSO.FileExists("C:\EXITCODE.DAT") Then
         Set oTS = oFSO.OpenTextFile("C:\EXITCODE.DAT", 1, False)
             szExitCode = oTS.ReadAll
             oTS.Close
         Set oTS = Nothing
         oFSO.DeleteFile("C:\EXITCODE.DAT", True)
         WScript.Echo "Exit Code: " & szExitCode
      Else
         WScript.Echo "Batch file produced no exit code"
      End If

It's either that or it may be time to convert that batch to VBS.

HTH

J.
Is see this an old question, however the OP didn't get an answer to below question:

---- SNIP ----
The WshShell.Run solution works like a charm, except that I can't seem to use a variable to exit the dos command script. IE:

set myerr = 2
EXIT %myerr%

does not work while:

EXIT 2 does.(?!?!)
---- SNIP ----


jimbobmcgee made a note about the environment being created/destroyed during running an external program. He is right in his observations, but the fact that this takes place does not impact the use of new environment variables in the code being run. %myerr% would be erased after delivering its value to the EXIT command.

The problem with that batch code is the whitespaces between "="

set myerr = 2

should be changed to:

set myerr=2

And it will work.
Thanks vb_elmar,

That's even better!

Rob
sharza that was, but youre welcome ;)