Skip to content
⌘ NSIS Forum Archive

Get return-statement of a executed batch.

6 posts

fyaa#

Get return-statement of a executed batch.

Hello,

I'm relatively new to working with the NSIS Installer. Most features are easy to handle and I get a success, but there is one thing which I didn't get a solution for.. I've been trying since yesterday morning, so hopefully you can help me with it 😉.

After installing files to the install-directory, I have to start a Batch-file with parameters to do some operations.
So I do either

ExpandEnvStrings $0 %COMSPEC%
ExecWait '"$0" /C "$SOMEDIRECTORY\my.bat" "quotedparameter" parameter2 parameter3' $0
MessageBox MB_OK "Exit code $0"
or

ExpandEnvStrings $0 %COMSPEC%
ExecDos::exec '"$0" /C "$SOMEDIRECTORY\my.bat" "quotedparameter" parameter2 parameter3' '' "$EXEDIR\stdout.txt"
Pop $0
MessageBox MB_OK "Exit code $0"
..but nothing works like I supposed.
With option 1 I get a 1 whatever I do on input.
With option 2 I get always a -.

When I start the batch exclusively, it returns either "OK" or "ERROR".
This "State" I have to return to my NSIS installer. Depending on it the Installer will throw an Error in a MessageBox or continue.

So, how am I supposed to do this?
Anders#
Batch file exit codes are problematic. Are you using exit %somenumber% in your batch file?

ExecWait uses the exit code for the comspec process (Internally cmd.exe will call ExitProcess() with some number and that is the number you get)
T.Slappy#
Please post your .bat file - or at least the most important part of it - that one which returns OK or ERROR. Use pastebin for this purpose 🙂
fyaa#
Hello again,

thanks for your anwsers.
It would be easier if I could modify the batch-filem but it's a batch file from an oracle database, so I can't modify it.
As an input I give this batch an tcl-file so that I can put an output like "OK", "ERROR" or something else.

Maybe I can encapsulate this propriate batch-file with a custom one and throw some error level depending on the output of the .. batch-file.

Like:

Call path/to/batch.bat tclskript.tcl input1 input2.. or write this output to a variable?..
if %variable% == "OK" (
Exit /B 1
) else (
Exit /B 0
)
.. or something like this.

But I'm neither experienced in batch-programming nor in NSIS-scripting/programming.
fyaa#
Well it works now:

Just use:


FileOpen $0 "$TEMP\test.bat" w
FileWrite $0 "SET OUTPUT=$\n"
FileWrite $0 "FOR /F $\"tokens=*$\" %%R IN ('$\"PATH\TO\my.bat$\"') DO SET OUTPUT=%%R$\n"
FileWrite $0 "IF %OUTPUT%==OK ($\n"
FileWrite $0 " exit /B 1$\n"
FileWrite $0 ") ELSE ($\n"
FileWrite $0 " exit /B 0$\n"
FileWrite $0 ")$\n"
FileClose $0
With:

ExpandEnvStrings $0 %COMSPEC%
ExecDos::exec /NOUNLOAD /ASYNC '"$0" /C "$TEMP\test.bat"'
Pop $0
ExecDos::wait $0
Pop $0
you have the errorcode in $0. So it's in this example either 1 oder 0 and you can react on it using ${if}.

🙂