Computing.Net > Forums > Programming > For loop do two things

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.

For loop do two things

Reply to Message Icon

Name: AJ (by William Jimenez)
Date: December 9, 2008 at 07:00:56 Pacific
OS: Windows XP
CPU/Ram: Intel Dual Core
Product: Dell / OPTIPLEX
Comment:

Hello,

I have a for lookp that checks through a list of computer names in a text file and attempts to ping each one twice.

for /f %i in (CompNames.txt) do Ping %i -n 2

what i would realy like to say is also that if %errorlevel%==0 or basically if the PC responds to ping then redirect %i to a text file.

I've added "& if %errorlevel%==0 echo %i >> Output.txt" but of course all it does is ping and also echo all the time.

Thanks in advance.




Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: December 9, 2008 at 07:12:36 Pacific
Reply:

When you check %errorlevel% inside the loop, %errorlevel% is converted to a number before the loop starts, and never changes value during the loop. You can use the command

setlocal enabledelayedexpansion

and !errorlevel! instead. But, more succinctly, just use IF NOT ERRORLEVEL 1.


0

Response Number 2
Name: lee123abc
Date: December 9, 2008 at 08:36:28 Pacific
Reply:

Hi Klint... I tried that suggestion and couldnt get it to work. Could you advise on what I am doing wrong here please? (I actually tried a few different variants.)
From the following example, all I get returned in Output.txt is "%i"

setlocal enabledelayedexpansion
for /f %%i in (Compnames.txt) do Ping %%i -n 2
if !errorlevel!==0 echo %%i >> Output.txt


0

Response Number 3
Name: AJ (by William Jimenez)
Date: December 9, 2008 at 08:48:36 Pacific
Reply:

It worked perfect for me. Thanks Klint


Hello lee123abc, I could be wrong but I think that at least above you are missing the () on after the DO command thus it shoudl be:

setlocal enabledelayedexpansion
for /f %%i in (Compnames.txt) do (Ping %%i -n 2
if !errorlevel!==0 echo %%i) >> Output.txt

I hope it was that simple.

Regards to all.


0

Response Number 4
Name: klint
Date: December 9, 2008 at 15:04:13 Pacific
Reply:

I think AJ is right.

Another way of doing it is this:

for /f %%i in (Compnames.txt) do (Ping %%i -n 2 && echo %%i>>Output.txt)

The echo command will only be executed if the Ping command succeeds.


0

Response Number 5
Name: AJ (by William Jimenez)
Date: December 9, 2008 at 17:02:09 Pacific
Reply:

Thanks Klint,

Is that what && means?

If you could describe && in a short sentence what would you say?

Thanks a lot.


0

Related Posts

See More



Response Number 6
Name: Holla
Date: December 9, 2008 at 21:51:54 Pacific
Reply:

AJ,

From
http://www.robvanderwoude.com/conde...

1. command1 & command2
means: Execute command2 after execution of command1 has finished
2. command1 && command2
means: Execute command2 only if execution of command1 has finished successfully
(execute command1
then, if not errorlevel 1 execute command2)
3. command1 || command2
means: Execute command2 only if execution of command1 has finished unsuccessfully
i.e.,
execute command1
then IF ERRORLEVEL 1 execute command2

--
Holla.


0

Response Number 7
Name: Mechanix2Go
Date: December 10, 2008 at 02:27:47 Pacific
Reply:

Attention shoppers,

In NT5 ping returns 0 even if the tarhet is stone dead.

====================================
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (IPlist) do (
ping %%a | find "Reply" > nul
if not errorlevel 1 echo %%a >> log
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 8
Name: lee123abc
Date: December 10, 2008 at 08:35:28 Pacific
Reply:

Thanks guys, brilliant help!

AJ - I tried your suggestion first and it worked a charm. I am going to try the other methods as well and try to figure how they work.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Autoinput y value and n f... Batch file troubles



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: For loop do two things

Display numbers with C++ for loops www.computing.net/answers/programming/display-numbers-with-c-for-loops/7449.html

Nested for loops to seperate data www.computing.net/answers/programming/nested-for-loops-to-seperate-data/19438.html

using for loops to interate unicode www.computing.net/answers/programming/using-for-loops-to-interate-unicode/12858.html