Hi All
I'm writing a batch file (and I'm a newbie so excuse any stupid questions) that will ping a number of locations and will report all the items it doesn't get a response from. Currently, when it gets no response, it created a variable with the name of the location. What I would like to do is have it display all the variables it created at the end of the file so that you can see what was unreachable.
I'm guessing to do this, I would need to either append to a variable - Eg., location=location+south (with south being the newly undiscoverable location)... and it would just keep adding on. But I don't know how to do this. And is it possible to separate them with their a comma or put them on a new line so they display better?
Or the other way is I thought each location will get it's own variable - Eg., location1=south, location2=north... and at the end it would just list location1, location2, etc. But how do I get it to not list anything for any variable that wasn't set as it wouldn't have been set it if was found? Does that make sense? Currently when I try for blank variables it says "Echo is off.". I don't want it to say anything for blank variables.
I'm not sure which way would be best but currently I can't work out how to do either. So any help would be greatly appreciated.
Thanks.
Batch File - Either Append or Hide Blank?
Moderator: DosItHelp
Re: Batch File - Either Append or Hide Blank?
Simplest I can think of is to append responding locations to a text file, e.g. PingTest.txt
You start by deleting PingTest.txt, and then work through your list of locations,
When you finish all locations, then PingTest.txt holds the results.
Just copy this code, and save it as PingTest.BAT
Invoke "PingTest 127.0.0.1" and you respond to yourself and append 127.0.0.1 to the results
Invoke "PingTest 127.0.0.0" and nothing responds, nothing is added to the results.
N.B.
The only line you need is
ping -w 40 -n 1 %1 | find "Lost = 0 " > NUL && echo %1 >> PingTest.txt
The argument %1 is the location to test/report.
%1 is valid for debug of manual operation of PingTest.bat,
or use %TEST_LOCATION% if you want to use a variable for each test.
The third line "type PingTest.txt" is merely for debug testing - not needed in final code
Alan
You start by deleting PingTest.txt, and then work through your list of locations,
When you finish all locations, then PingTest.txt holds the results.
Just copy this code, and save it as PingTest.BAT
Code: Select all
@echo off
ping -w 40 -n 1 %1 | find "Lost = 0 " > NUL && echo %1 >> PingTest.txt
type PingTest.txt
Invoke "PingTest 127.0.0.1" and you respond to yourself and append 127.0.0.1 to the results
Invoke "PingTest 127.0.0.0" and nothing responds, nothing is added to the results.
N.B.
The only line you need is
ping -w 40 -n 1 %1 | find "Lost = 0 " > NUL && echo %1 >> PingTest.txt
The argument %1 is the location to test/report.
%1 is valid for debug of manual operation of PingTest.bat,
or use %TEST_LOCATION% if you want to use a variable for each test.
The third line "type PingTest.txt" is merely for debug testing - not needed in final code
Alan
-
- Posts: 5
- Joined: 30 Mar 2010 18:07
Re: Batch File - Either Append or Hide Blank?
Thanks - that is actually what I sort of ended up doing. I searched and found that I could append to a text file so I used that method.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: Batch File - Either Append or Hide Blank?
I use the location1, location2 method with delayedexpansion and a for loop sorta like this:
setlocal enabledelayedexpansion
set idx=0
...other code
...when you find an error:
set /a idx+=1
set location%idx%=whatever
..and continue on.
...at the end you can:
for /l %%a in (1,1,%idx%) do echo !location%%a!
setlocal enabledelayedexpansion
set idx=0
...other code
...when you find an error:
set /a idx+=1
set location%idx%=whatever
..and continue on.
...at the end you can:
for /l %%a in (1,1,%idx%) do echo !location%%a!