Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
OK, here's one that's driving me crazy....
If I have a Windows batch script where I need to do two things:
1. Invoke a command and capture the output into a variable.
2. KNOW what the ERRORLEVEL is after that command is invoked.how do I do this with the FOR /F command?
For example, If I have this FOR command:
for /F "usebackq delims=€" %%G in (`"%JAVA_CMD%"`) do (
echo %ERRORLEVEL%
set _stdout=%%G
)(..and assume JAVA_CMD is a valid java commandline that returns a non-zero exit code)
the echo %ERRORLEVEL% ALWAYS echos a value of 0, regardless of the actual value exited by the java program. I am not having issues capturing stdout of the java program, it's the ERRORLEVEL that is vaporizing.Any help on this one would be greatly appreciated.

The problem your facing relates to the way variables are expanded in batch.
If you only need to check for very specific errorlevels (usually more than 1 or 0) the old style of errorlevel checking will work:
if errorlevel 1 ( echo Errorlevel is greater or equal to 1 ) else ( echo Errorlevel is less than 1 )Just be aware that it is *sometimes* possible to have a negative errorlevel and the style above tests if the errorlevel is >= number. You could always nest if statements to be certain zero was returned.
If you need the errorlevel as a variable / pain number the how to on the other side of the link below should help(the nested variable part may seem off topic but can also be helpful in some cases):

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |