truncate log with robocopy

2.1k views Asked by At

I am running a robocopy process, and generating a log as follows:

robocopy "\\server1\reports" "c:\temp" /LOG+:"c:\scripts\logs\robolog-reports.txt"

The log file is created fine. Is there any way to limit the file size of the log file, ideally rolling the log so that only the newest XX lines or XX bytes are saved? I would like to do this all in one action (robocopy), and have the last several days of robocopy activity.

Thanks

3

There are 3 answers

1
Aacini On BEST ANSWER

The process of get the last N lines from a file is called tail. Try this:

@echo off
setlocal

rem Update the robocopy report file
set "report=c:\scripts\logs\robolog-reports.txt"
robocopy "\\server1\reports" "c:\temp" /LOG+:"%report%"

rem Keep the last N lines in the report
set lines=300
for /F %%a in ('find /C /V "" ^< "%report%"') do set "lastLine=%%a"
if %lastLine% leq %lines% goto :EOF
set /A firstLine=lastLine-lines
more +%firstLine% "%report%" > tempFile.tmp
move /Y tempFile.tmp "%report%"

There is no way to do this directly in one robocopy action.

0
Matthew Lock On

I have found that because robocopy retries 1,000,000 times by default it can make the Robocopy log files grow huge if there's any errors by filling it with error messages. By limiting the number of retries (say with switch /r:3) it can make the log files grow much more slowly:

Why does robocopy retries 1000000 times?

0
georgk111 On

have the same problem, solved it with following script:

echo off
chcp 1252
type %1| find "Gestartet:"  > robocopy_summary.log
type %1| find "Quelle :"    >> robocopy_summary.log
type %1| find "Ziel :"      >> robocopy_summary.log
type %1| find "Optionen:"  >> robocopy_summary.log
echo "-----------------------------------------------------------------------------" >> robocopy_summary.log
type %1| find "Insgesamt"  >> robocopy_summary.log
type %1| find "Verzeich.:" >> robocopy_summary.log
type %1| find "Dateien:"   >> robocopy_summary.log
type %1| find "Bytes"      >> robocopy_summary.log
type %1| find "Zeiten"     >> robocopy_summary.log
type %1| find "Geschwindigkeit:" >> robocopy_summary.log
type %1| find "Beendet:"   >> robocopy_summary.log
echo "-----------------------------------------------------------------------------" >> robocopy_summary.log

Robocopy needs to be run by windows sceduler, say every 60 Minutes, the last command in the robocopy batchfile pointing to the above script "robocopy_summary.bat logfilename"

Not very smart, but doing all I want to see.