Question

I am struggling at a batch file errorlevel return issue, and could not find the answer.
Could any one give me any hints?

I have an external binary file, external.exe which read the return code (0-success, other-fail) of a batch file to decide what to do.

Within the batch file, mybatch.cmd, I wanted to run a script (perl) and return the result to the batch file.

For example, mybatch.cmd:

@echo off
myperl.pl
exit %errorlevel%

While myperl.pl is:

#!perl.exe
exit 0;

So I wanted myperl.pl to return value 0 to the batch file, and then the batch file return 0 to the external.exe file.

But external.exe didn't work as expected.

1) If I run mybatch.cmd from CLI, and then "echo %errorlevel%" from CLI, I could see it is 0.

2) If I add a line to display the return code from myperl.pl, I could see the return code from myperl.pl is exactly 0.

For example:

@echo off
myperl.pl
echo %errorlevel%
REM exit %errorlevel%

It will display 0, so the perl script returns the value exactly.

3) If I add a line to manually set the return code to 0, the external.exe works fine.

@echo off
myperl.pl
result=0
exit %result%

That is really weird. Any ideas? I could not control the external.exe, so I could not do debug on it.

In this example, the %errorlevel% is called twice:
1) Return from myperl.pl to batch.
2) Return from mybatch.cmd to external.exe. Will that be an issue?

Was it helpful?

Solution 2

Found out the root cause finally.

myperl.pl should be with fullpath, otherwise the external.exe didn't know where the myperl.pl was.

OTHER TIPS

All you have said is right: myperl.pl return a certain errorlevel, the Batch file take it and then it returns the appropriate errorlevel value. The problem is this: the Batch file, to what process return its errorlevel?

This scheme always works as a parent-children relation: a parent process execute a children one and waits for it to terminate, then the parent may take the errorlevel value returned by the children. The way to solve your problem is if the external.exe program execute the Batch file (cmd.exe) as a children! However, if you "could not control the external.exe", then there is no way to solve your problem...

How do you know that the external.exe program "read the return code (0-success, other-fail) of a batch file to decide what to do"? I think you have a confusion at this point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top