Batch file output to Log file

When running a batch file it might be beneficial and necessary to have a log file detailing the status of the commands. While there are different ways around creating a log file, batch file redirection is the key.

To explain redirection lets see this example of a batch file that shutdowns some computers:

shutdown -s -f -m \\Computer01 -c  >> shutdownlog.txt 2>&1 shutdown -s -f -m \\Computer02 -c  >> shutdownlog.txt 2>&1 shutdown -s -f -m \\Computer03 -c  >> shutdownlog.txt 2>&1

If you look at the end of the command i have added the following:

>> - Will append the log file.

shutdownlog.txt -The name given to the log file.

2>&1 - Displays standard output and standard error of command on the log file.

At the end of your batch file you can also add a date & time stamp:

echo %date% %time% >> shutdownlog.txt

Great page on Batch file redirection - http://www.robvanderwoude.com/redirection.php


Tagged under: scripts, batch file, log, automated