问题
Is there anyway to do the below without delayed expansion (one line, broken for readability)?
%comspec% /v:on /c %windir%\System32\reg.exe import c:\temp\test.reg &
if !errorlevel! neq 0 pause
If it were an echo, I know that call
can be used, but doesn't seem to be available for use with the if
.
回答1:
One way is to use this older syntax:
%comspec% ... & if errorlevel 1 pause
In this case, a special variant of if
statement is used, one that tests the current errorlevel state.
Another option might be this:
%comspec% ... || pause
The ||
command delimiter is roughly equivalent to & if !errorlevel! neq 0
, i.e. the subsequent command is executed only if the previous one terminated with a non-zero exit code.
回答2:
You could use conditional execution:
reg.exe import c:\temp\test.reg || pause
||
means execute on failure (ie errorlevel neq 0).
You may may also want to try this example to see how it works:
find "this" && echo found || echo not found
- it will inform you if 'this' was in input stream (entered from keyboard unless you redirect).
来源:https://stackoverflow.com/questions/14418960/dos-inline-if-test-for-errorlevel-without-use-of-delayed-expansion