Is there a way, within a batch file, to log the error and success of the batch file?
I am trying to copy a file from my computer to 200 machines (works great) but want to know which fail and the reason it fails (because the screens go away really quick).
@echo off
if [%1]==[] goto usage
@echo mapping l: to %1\c$
net use * /delete /y
net use l: \\%1\c$ password /user:%1\administrator
if ERRORLEVEL 1 (
echo failed
net use l: \\%1\c$ password /user:%1\administrator
) else (
goto mappingError
) > command.txt
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo copying link file to C: Drive
copy "c:\_\CopyFileToHost\logoff.cmd" l:\
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo deleting l: mapping
net use l: /delete /y
@echo off
goto :eof
:usage
@echo Usage: %0 hostname
exit /B 1
:mappingError
@echo Error mapping remote drive, exiting
exit /B 1
This is simple logging - just checks if the drive was mapped and if the filename appears on the mapped drive, and writes the log to your desktop.
First, don't use
because the square brackets don't have any special meaning here. Better is using
The double quote character has a special meaning because it makes it possible to compare strings containing 1 or more spaces or 1 or more of the characters
&()[]{}^=;!'+,`~
.%~1
means first argument with surrounding double quotes removed. See help output by running in a command prompt windowcall /?
for more details.Second, read the Microsoft articles about Using command redirection operators and Testing for a Specific Error Level in Batch Files to understand the batch code below.
Error messages written to stderr are appended to stdout by
2>&1
to which standard messages are written by default. And everything written to stdout is redirected to filecommand.txt
in current directory and therefore the text file contains standard and error messages in right output order.If you want the commands also written to the text file to easier determine from which command which error or standard message comes,
echo
additionally the commands, too.