Page 1 of 1

Batch File - Either Append or Hide Blank?

Posted: 29 Apr 2010 17:33
by fatal.lordes
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.

:)

Re: Batch File - Either Append or Hide Blank?

Posted: 30 Apr 2010 09:29
by alan_b
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

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

Re: Batch File - Either Append or Hide Blank?

Posted: 30 Apr 2010 15:21
by fatal.lordes
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.

:)

Re: Batch File - Either Append or Hide Blank?

Posted: 03 May 2010 09:24
by avery_larry
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!