1

Running this batchfile from the Windows commandline results in an %errorlevel% of 5 (i.e. running echo %errorlevel% on the commandline after having executing the batch file prints the number 5):

EXIT /B 5

This is fine.

However, running this batchfile results in an %errorlevel% of 0, no matter what:

sleep 1
EXIT /B 5

I want it to return the error code 5. How can I do that?

Note: If I add sys.exit(13) to the sleep.py (see below), then the second batch file will return with an exit code of 13. So my batch file will return with the exit code of the sleep.py script instead of the exit code specified via the EXIT command (which is strange).


sleep.bat:

sleep.py %1

sleep.py:

import sys
import time

if len(sys.argv) == 2:
    time.sleep(int(sys.argv[1]))
1
  • No, I'm echoing from the commandline. So on the commandline I first enter "my_batch_script.bat" and let it run. Then I enter "echo %errorlevel%". I just tried delayed expansion but it doesn't seem to work on the commandline at all (echoing !errorlevel! just prints !errorlevel!). Commented Jul 16, 2014 at 12:52

1 Answer 1

3

sleep is a batch file. When calling a batch file from another batch file this way, control never returns to the caller (akin to exec in POSIX). Use call sleep 1 instead.

Or just sleep with built-in programs:

:sleep1
setlocal
set /a X=%~1 + 1
ping ::1 -n %X% >nul 2>&1
endlocal
goto :eof

:sleep2
timeout /T %1 /nobreak
goto :eof
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.