Computing.Net > Forums > Programming > ERRORLEVEL when using FOR /F

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

ERRORLEVEL when using FOR /F

Reply to Message Icon

Name: tvattima
Date: October 27, 2009 at 13:26:55 Pacific
OS: Windows XP
Product: Microsoft Windows server 2003 enterprise
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 27, 2009 at 14:16:57 Pacific
Reply:

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):


Batch Variable how to


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: ERRORLEVEL when using FOR /F

Surpress error using for /f reg www.computing.net/answers/programming/surpress-error-using-for-f-reg/17821.html

Inserting File name into mult. file www.computing.net/answers/programming/inserting-file-name-into-mult-file/17159.html

Batch File: Copy part of a text to a new file www.computing.net/answers/programming/batch-file-copy-part-of-a-text-to-a-new-file/19723.html