Computing.Net > Forums > Programming > Batch, line by line files.

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, line by line files.

Reply to Message Icon

Name: Critical
Date: July 2, 2009 at 11:11:59 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 2, 2009 at 12:15:58 Pacific
Reply:

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


0

Response Number 2
Name: Critical
Date: July 2, 2009 at 12:18:55 Pacific
Reply:

Interesting way of doing it.
Are your, 'can ping' and 'cannot ping' labels reversed?


0

Response Number 3
Name: Mechanix2Go
Date: July 2, 2009 at 12:45:00 Pacific
Reply:

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


0

Response Number 4
Name: Critical
Date: July 2, 2009 at 14:15:32 Pacific
Reply:

My mistake, works perfectly, but I dont know why it creates a new file called 'newfile' whenever I run it.


0

Response Number 5
Name: Mechanix2Go
Date: July 3, 2009 at 01:26:38 Pacific
Reply:

Chamge the first line to:


@echo off & setLocal enableDELAYedexpansion


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

M2


0

Related Posts

See More



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, line by line files.

XP batch script to rename files www.computing.net/answers/programming/xp-batch-script-to-rename-files/14608.html

Send email+attachment by bat file www.computing.net/answers/programming/send-emailattachment-by-bat-file/16774.html

batch to rename txt file using data inside www.computing.net/answers/programming/batch-to-rename-txt-file-using-data-inside/19709.html