Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Return code from WshShell.Exec

3,934 views
Skip to first unread message

Andy

unread,
Jun 28, 2004, 2:20:08 PM6/28/04
to
Hi all!

I am running a test script and gets always the retun code 0,
even the batch program has errorlevel <> 0

Set WshShell = WScript.CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("d:\testscript\test.bat " & "1.txt" & " 2.txt")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
MsgBox oExec.Status & " " & oExec.ExitCode

Thank you.

Tom Lavedas

unread,
Jun 28, 2004, 4:55:01 PM6/28/04
to
I'm afraid you'll need to show us the actual batch procedure to troubleshoot this. At this point it's impossible to tell why you are getrting that result, even though you claim your batch procedure is returning a non-zero result.

Also, what OS. AFAIK, Win9x/Me versions do not return error codes to WSH from batch procedures, because their command processor, Command.com, is not instrumented to do it.

Tom Lavedas
===========

Andy

unread,
Jul 2, 2004, 12:32:01 PM7/2/04
to
Hi Tom!

I still have the same problem. I tried on W2K SP4, Windows XP SP1
It returns 0 in RunBatch script
even test_exec.bat return 1.
if you uncomment the last two lines in test_exec.bat and runt it
separately it will report errorlevel 1, but
if it is called by runBatch.vbs, MsgBox will display 1, 0 (Status,
ExitCode)


********************************runBatch.vbs*************************************************************
RunBatch

Function RunBatch


Set WshShell = WScript.CreateObject("WScript.Shell")

Set oExec = WshShell.Exec("D:\testscript\test_exec.bat " & "1.txt"
& " 2.txt")

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

MsgBox "Status: " & oExec.Status & Chr(13) & "ExitCode: " &
oExec.ExitCode


End Function
***********************************************************************************************************

********************************test_exec.bat**************************************************************
@echo off
echo no | find "yes"
rem echo %errorlevel%
rem pause
***********************************************************************************************************

Thank you

"Tom Lavedas" <tlav...@hotmail.remove.com> wrote in message news:<0BF2D3D7-B2E6-4513...@microsoft.com>...

Tom Lavedas

unread,
Jul 2, 2004, 1:05:01 PM7/2/04
to
It's exactly as I thought. The ERRORLEVEL at the end of your batch procedure IS zero, not 1.

It is zero because under Cmd.exe the REM statement is a fully quallified statement that returns
an ERRORLEVEL (always zero), just like ECHO (almost always zero) and the external FIND
(dependent on results). Try changing the test batch to EXACTLY ...

@echo off
echo no | find "yes"

And see what the result is. Then add an ECHO %ERRORLEVEL% or a REM.

The only 'safe' remark in a WinNT/Cmd.exe environment is to use a colon followed by another
punctuation mark - commonly another colon. For Example, ...

:: This is a remark that has no effect on ERRORLEVEL
REM This one resets it to zero

a...@nyangau.fsnet.co.uk

unread,
Jan 20, 2005, 5:48:04 AM1/20/05
to
Actually, I have a batch file I am _sure_ is returning a non-zero
ERRORLEVEL
(If I run it by hand and echo %ERRORLEVEL% afterwards, I can see this).
But the ExitCode property is returning 0.
Problem is seen on Windows XP.

{{{ Andy

a...@nyangau.fsnet.co.uk

unread,
Jan 20, 2005, 6:02:48 AM1/20/05
to
More than this, it works with WSH 5.6 on Windows NT4, or if you execute
an .EXE file that returns a non-zero return code.

So - is it a bug in the WSH on XP, when handling .bat files.
Presumably the Exec method must recognise this case and handle it
differently to .EXEs.

Torgeir Bakken (MVP)

unread,
Jan 20, 2005, 6:47:52 AM1/20/05
to
a...@nyangau.fsnet.co.uk wrote:

Hi

Use "exit %errorlevel%" in your batch file.

This batch file

--------------------8<----------------------
@echo off
:: do something that sets errorlevel 1
copy sdlfjasd sdlfjasd >nul
exit %errorlevel%
--------------------8<----------------------


run with this VBScript:

--------------------8<----------------------
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("c:\6\test.bat")


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

WScript.Echo oExec.ExitCode
--------------------8<----------------------

gives me 1 as output on my WinXP computer.

--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

a...@nyangau.fsnet.co.uk

unread,
Jan 20, 2005, 3:44:27 PM1/20/05
to
Changing the .bat file I want to call is undesirable (though possible),
as it is a vendor supplied script. It is the WebSphere 5 wsadmin.bat,
which
includes code like this at the end

setlocal
java something
endlocal

(Before anyone asks, if java returns non-zero errorlevel, the endlocal
does not undo it, as can be confirmed by running the script and
manually using

if errorlevel 1 echo stillerror

afterwards.

I had read about using exit /B 1, but this didn't work for me when I
tried it.

I noticed that all of the lines of a Exec-ed .bat are echoed before any
of them are executed, and I didn't expect this.
Could WSH itself be involved in the step by step interpretation of the
.bat file.
I thought the multi-line aspect of things might be the problem.

So I wrote callbat.bat, which looks like this

@echo off
call %1 %2 %3 %4 %5 %6 %7 %8 %9

I now Exec("callbat.bat actual.bat arg1 arg2"), and guess what, it
works every time!

{{{ Andy

Gerry Hickman

unread,
Jan 20, 2005, 5:11:09 PM1/20/05
to
a...@nyangau.fsnet.co.uk wrote:

> setlocal
> java something
> endlocal

I don't see any specific exit code here? As far as I know you can only
see the retval from exec if you specify exit.

> I had read about using exit /B 1, but this didn't work for me when I
> tried it.

Did you read about it on a non-Microsoft site (as I did)? If so I can
confirm /B does NOT work, but using 'exit 1' or 'exit %ErrorLevel%' does.

> I noticed that all of the lines of a Exec-ed .bat are echoed before any
> of them are executed, and I didn't expect this.

Does the bat begin with '@echo off'?

> Could WSH itself be involved in the step by step interpretation of the
> .bat file.

As far as I know it executes it as a process just like any other file.


--
Gerry Hickman (London UK)

0 new messages