Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

batch file test error level

I'm trying to conditionally run an exe from a batch file conditionally upon another exe executing successfully.

I've tried a few different combinations of IF and ERRORLEVEL but none seem to work

"......TeamBuildTypesCurrent BranchDatabaseUpdate.exe" -s localhostsql2008r2 

IF %ERRORLEVEL% 1(
"......TeamBuildTypesCurrent BranchDatabaseUpdate.exe" -s localhostsql2008
)
Pause

Gives me the error

1( was unexpected at this time.

Where am I going wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

IF ERRORLEVEL ... is a special syntax supported since the DOS days, the %ERRORLEVEL% variable support was added in WinNT.

The original syntax is used like this:

call someapp.exe
if errorlevel 1 goto handleerror1orhigher
echo succuess... 

To use the variable, use the normal IF syntax: if %errorlevel%==0 echo success...

Note that %errorlevel% stops working if someone does set errorlevel=foo and it might not get updated for internal cmd.exe commands.

An alternative solution is to use &&:

call someapp.exe && (echo success) || (echo error!)

There are (at least) two known cases where errorlevel is broken and you must use || instead:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...