Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Batch FOR Loop %ERRORLEVEL%

Original Message
Name: jboswell
Date: October 28, 2005 at 12:56:10 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
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


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: October 28, 2005 at 13:21:34 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 2
Name: IVO
Date: October 28, 2005 at 13:24:50 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
Sorry... and replace %ErrorLevel% with !ErrorLevel! as the delayed variable is marked with the ! symbol instead of %

Report Offensive Follow Up For Removal

Response Number 3
Name: dtech10
Date: October 28, 2005 at 13:30:24 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
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
)


Report Offensive Follow Up For Removal

Response Number 4
Name: dtech10
Date: October 28, 2005 at 13:32:56 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
Hi
Sorry IVO posted just before me.
But he's hit the nail on the head.


Report Offensive Follow Up For Removal

Response Number 5
Name: jboswell
Date: October 28, 2005 at 17:01:56 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
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


Report Offensive Follow Up For Removal


Response Number 6
Name: jboswell
Date: October 31, 2005 at 10:22:35 Pacific
Subject: Batch FOR Loop %ERRORLEVEL%
Reply: (edit)
Worked like a charm! Thanks for the help!

Joe


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch FOR Loop %ERRORLEVEL%

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC