Set errorlevel from batch-file

2020-04-17 03:50发布

问题:

I am working on this complicated (for me...) automation and one part is a batch script Started (not Called) from another one.

Now, it's all work-in-progress and I'd like to be able to replace the Started batch script with a simple instruction that would do such that the Start-ing script, after having started the child script, has:
%ERRORLEVEL% EQU 1 (And then I could work on the error-handling aspects and test stuff)

But also something nice that doesn't put the system in an unnatural state (and I might script things according to that bad state and wonder why things don't work when I assemble it all).
So, "Format C -q" doesn't work! ;)

Thanks

回答1:

cmd /c exit 1 will set the errorlevel to 1.



回答2:

If you want to have user input errorlevel you can use the choice command.

@echo off
choice /c 123456789
echo %errorlevel%

Depending on what you type on the order of the choice command the errorlevel will be set starting at 1.

example:

choice /c dghet

pressing 'd' will make the errorlevel 1, and pressing h will make the errorlevel 3. Also as a perk you don't need to press enter after the input.