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.
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
===========
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>...
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
{{{ Andy
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.
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
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
> 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)