@echo offDon't echo these batch file commands to the screen. Using the @ sign means including the current line.
if exist d:\tools\computers.txt goto Label1If there is a file called
d:\tools\computers.txt then jump to the section of the batch file called
:Label1echo.
Type a dot to the screen.
echo Cannot find d:\tools\computers.txtAs the file called
d:\tools\computers.txt was not found then type "
Cannot find Etc." to the screen. This file must exist in the location specified. It presumably contains a list of machine names or IP addresses; one per line.
PausePut a prompt on the screen which says "Press any key to continue..." and then wait until a key is pressed.
goto :eofGo to the end of the batch file (i.e. terminate)
:Label1
Now there is a subsection called
Labelecho PingTest executed on %date% at %time% > d:\tools\z.txt
Type the words "
PingTest executed on " along with the current date and time " but instead of typing them to the screen, pass them (using the
> symbol) to a file called
d:\tools\z.txtfor /f %%i in (d:\tools\computers.txt do call :Sub %%iThere's actually a bracket missing from this, so the correct line should read:
for /f %%i in (d:\tools\computers.txt) do call :Sub %%i
Loop through each line of the file
d:\tools\computers.txt which presumably contains a list of machine names or IP addresses. Call a subroutine called
:Sub passing it the line that's just been read. For example this file might look like:
193.1.1.1
193.1.1.2
193.1.1.200
notepad d:\tools\z.txt
The file
d:\tools\z.txt is a log produced by the batch file. This line will launch NotePad with this file so you can view it.
goto :eof
Go to the end of the batch file (i.e. terminate)
:Sub
This is the subsection call Sub which gets called by the For loop.
echo Testing %1
Type to the screen the line that has been passed to this subsection. For example, "Testing 193.1.1.1"
set state=alive
Create a temporary value called "state" which is set to "alive"
ping -n 1 %1 | find /i "bytes=" || set state=dead
Call the PING executable. If you want to know more about this command then type PING /? from the command line. The output from this is then "piped" to the FIND executable which is set to ignore case (/i). Essentially it's looking for the PING command to be returning the text "bytes=" meaning that the IP address or machine name is responding to the ping. If the text is not found (so the find command failed) then the "state" value is changed from "alive" to "dead".
echo %1 is %state% >> d:\tools\z.txt
Type the IP address and the state value (dead or alive) to the file d:\tools\z.txt . The presence of the two > characters means append rather than overwrite.
There is some very clever batch file stuff in here, especially the use of the FOR command calling the subroutine and passing it a line at a time read from the text file, and then in the subroutine the use of the output piped from the PING command into the FIND command and then setting the state value if the find fails.
Very underrated things batch files.
-
Marked as answer by
IamMredMicrosoft employee, Owner
Sunday, January 17, 2010 8:32 AM