Windows Batch Files: Logging & more
July 24, 2020

Windows Batch Files: Logging & more

To log all output of a batch file to a file,  you can do:

@echo off

rem https://stackoverflow.com/questions/203090/how-do-i-get-current-datetime-on-the-windows-command-line-in-a-suitable-format
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
rem echo %day%
rem echo %DayOfWeek%
rem echo %hour%
rem echo %minute%
rem echo %month%
rem echo %quarter%
rem echo %second%
rem echo %weekinmonth%
rem echo %year%

call :LOG > D:\Humi\Backup\Humi-backup-full-%year%-%month%-%day%_%hour%%minute%%second%.log
echo SUCCESS!
echo on
exit /B

:LOG
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_BackupDatabases @backupType='F'"
robocopy D:\Humi\Backup \\10.0.150.15\Backup\CSARNOK-ThinkCentre1 /XC /XN /XO /zb

References:

How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
Update: Now that it’s 2016 I’d use PowerShell for this unless there’s a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using date. See @
Log an entire batch file output
I’m using a command line script to adjust multiple settings on a computer. However, I would like to have the entire output to also be logged into a .txt or .log file. When I use my basic knowledge...
Windows Batch Files: Logging & more
Share this