Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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.

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

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.txtI hope it was that simple.
Regards to all.

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.

Thanks Klint,
Is that what && means?
If you could describe && in a short sentence what would you say?
Thanks a lot.

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.

Attention shoppers,
In NT5 ping returns 0 even if the tarhet is stone dead.
====================================
@echo off & setLocal EnableDelayedExpansionfor /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

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.

![]() |
Autoinput y value and n f...
|
Batch file troubles
|

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