Batch, line by line files.

July 2, 2009 at 11:11:59
Specs: Windows XP
Hi there! Scripting a program for work but I am new to batch files and cannot get this segment to work.

@echo off
set N=
for /f "tokens=* delims=" %%a in (computers.txt) do (
ping /n 1 %%a | find "Reply" >nul
if %errorlevel% == 0 goto reply
@echo %%a | Cannot ping
goto end
:reply
@echo %%a | Can ping
:end
)
pause

Intention: To get the list of computer names from 'computer.txt' and ping them replying, yes or no.

Problem: When I pull out the middle part in the loop and run it alone, it works fine. When I pull out the loop and run it alone, it works fine. When I combine it for some reason it just does not let me use %%a as a variable for a computer name.

Thanks!


See More: Batch, line by line files.

Report •


#1
July 2, 2009 at 12:15:58
A goto in a for loop is usually problematic. Try this:

===========================
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims=" %%a in (computers.txt) do (
ping /n 1 %%a | find "Reply" > nul
if errorlevel 1 (
echo %%a NG
) else (
echo %%a OK
)
)


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

M2


Report •

#2
July 2, 2009 at 12:18:55
Interesting way of doing it.
Are your, 'can ping' and 'cannot ping' labels reversed?

Report •

#3
July 2, 2009 at 12:45:00
I don't think so. I get this:

127.0.0.1 OK
0.0.0.0 NG


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

M2


Report •

Related Solutions

#4
July 2, 2009 at 14:15:32
My mistake, works perfectly, but I dont know why it creates a new file called 'newfile' whenever I run it.

Report •

#5
July 3, 2009 at 01:26:38
Chamge the first line to:


@echo off & setLocal enableDELAYedexpansion


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

M2


Report •


Ask Question