Explain this batch file, please

bdincode

Beta member
Messages
1
Location
USA
Hello!

If someone could explain the parts of this batch file to me I would appreciate it greatly. I use it every morning when I get in the office to test connectivity of important servers.

I understand what the purpose is, just not how it works, as in what each line does.

Thanks in advance!

---BEGIN CODE---

@echo off
set fnm=C:\Users\xxxxxxx\Desktop\PingBatchFile\shortList.txt
set lnm=C:\Users\xxxxxxx\Desktop\PingBatchFile\pingOutcome.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo PingTest STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo PingTest ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=alive
ping -n 1 %1
if errorlevel 1 set state=dead
echo %1 is %state% >> %lnm%


---END CODE---
 
Intro info:
echo prints to the screen
echo. prints a blank line
anything enclosed in % signs are variables
the > character "pipes" it to another command; usually used for logging purposes

Line 1 turns off console output, so that the commands aren't printed to the screen, and only the text used with "echo" is displayed.
Lines 2 and 3 set 2 variables as 2 text files. Looks like %fnm% is the list of systems it's pinging, and %lnm% is basically a log file of what each ping command's outcome was.
Line 5 checks if the file defined as %fnm% exists. If it does, it jumps to the marker Label1 and continues with the batch file. If it doesn't find it, it prints that it can't find the file, waits for the user to press enter (the Pause command does this), and then jumps to the marker "eof" which is the end of the program (eof = end of file).

In Label1, it starts the ping test subroutine (marker :Sub). It prints the date/time, as well as logging it out to the file. Looks like it's looping with a for loop, and calls the :Sub marker, which prints the system its pinging, and then pings it. It checks the error level, and then outputs the results (either success or fail) to the logfile.

After all the systems have been looped through in the for loop, it prints/logs that the PingTest ended, and that it's exiting. It then jumps to the eof (end of file) and exits the batch script.
 
Back
Top Bottom