Computing.Net > Forums > Programming > Batch FOR Loop %ERRORLEVEL%

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.

Batch FOR Loop %ERRORLEVEL%

Reply to Message Icon

Name: jboswell
Date: October 28, 2005 at 12:56:10 Pacific
OS: Windows XP
CPU/Ram: Lots
Comment:

Here is the code:

for /f %%Q in (machines.txt) do (
ping -n 1 -w 3 %PingThis%
IF %ERRORLEVEL% == 0 echo %%Q >> alive.txt ELSE echo %%Q Failed Ping >> dead.txt
)

Now, the problem is this. In machines.txt is 3 names:

koala
joe
solitude

"joe" is not an actual computer, so when the second loop goes to ping "joe" it will fail. Normally on the command line, when a ping fails, it sets %ERRORLEVEL% to 1. In a batch file WITHOUT a FOR loop, it will work but when I try to query %ERRORLEVEL% and sort which machines I can ping and which I can't, and I am trying to see if "joe" failed, I can't. When "joe" gets pinged and FAILS, it doesn't return a success/failure code to %ERRORLEVEL% at all.

What am I doing wrong!?!

JB



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 28, 2005 at 13:21:34 Pacific
Reply:

You must enable the "Delayed Expansion of Variables" otherwise in a For loop the environment variables retain their first value i.e they DON'T change.

So at the beginning of your code set

SetLocal EnableDelayedExpansion

and before exiting from the script code

EndLocal

That just removes your lock.


0

Response Number 2
Name: IVO
Date: October 28, 2005 at 13:24:50 Pacific
Reply:

Sorry... and replace %ErrorLevel% with !ErrorLevel! as the delayed variable is marked with the ! symbol instead of %


0

Response Number 3
Name: dtech10
Date: October 28, 2005 at 13:30:24 Pacific
Reply:

Hi
Not Exactly Sure but try this, not knowing
what's in %PingThis%

for /f %%Q in (machines.txt) do (
ping -n 1 -w 3 %PingThis%
IF %ERRORLEVEL% == 0 IF NOT %ERRORLEVEL%==1 echo %%Q >> alive.txt ELSE echo %%Q Failed Ping >> dead.txt
)

or this

for /f %%Q in (machines.txt) do (
ping -n 1 -w 3 %PingThis%
IF ERRORLEVEL 0 IF NOT ERRORLEVEL 1 echo %%Q >> alive.txt ELSE echo %%Q Failed Ping >> dead.txt
)

or this

for /f %%Q in (machines.txt) do (
ping -n 1 -w 3 %PingThis%
IF NOT %ERRORLEVEL% == 1 echo %%Q >> alive.txt ELSE echo %%Q Failed Ping >> dead.txt
)


0

Response Number 4
Name: dtech10
Date: October 28, 2005 at 13:32:56 Pacific
Reply:

Hi
Sorry IVO posted just before me.
But he's hit the nail on the head.


0

Response Number 5
Name: jboswell
Date: October 28, 2005 at 17:01:56 Pacific
Reply:

Wow guys, that about does it right there. I will plug all this stuff in, and thanks for the VERY prompt reply!

And sorry about the %pingthis% thing that should have been %%Q...

Joe


0

Related Posts

See More



Response Number 6
Name: jboswell
Date: October 31, 2005 at 10:22:35 Pacific
Reply:

Worked like a charm! Thanks for the help!

Joe


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch FOR Loop %ERRORLEVEL%

Batch FOR loop and assigning variab www.computing.net/answers/programming/batch-for-loop-and-assigning-variab/16369.html

Batch for loop tokens www.computing.net/answers/programming/batch-for-loop-tokens/16727.html

XP Pro Batch Script -FOR loop issue www.computing.net/answers/programming/xp-pro-batch-script-for-loop-issue/16073.html