开发者

Generating managable log files on a batch file job

I am running a robocopy job through a batch file that is scheduled to run every hour. I am using the following switches:

Robocopy \servername\share D:\somedir /E /R:5 /Log+:D:\Logs\CopyLog.txt

It has been working faithfully for a long time but it ran into a snag this morning. I went to check the log file and was suprised to find it had grown to just shy of 280 Mb.

My question then is how I can change my batchfile to write more managable log files? I think it would make sense to append on a period for which it is reported in. ie: CopyLog_201103.txt would be the log file for March 2011.

I'm fairly adept at working with higher level programming languages, but command line programming was already long gone by the time I was growing up. I'm definitely learning the value of getting a firm understanding of the much more basic computer concepts.

Any sugg开发者_运维技巧estions/advice would be appreciated, thanks in advance.


Ugly way (not recommended, but needed on Windows 2000 and earlier):

set MyDate=%DATE:-=%
set Filename=D:\Logs\CopyLog_%MyDate:~0,6%
robocopy ... /Log+:%Filename%

Note: This assumes ISO-8601 as the short date format on your system. If that's not the case, then you need to construct the date from substrings of %date%.

Locale-proof way:

for /f "skip=1" %%d in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%d
set Filename=D:\Logs\CopyLog_%MyDate:~0,6%
robocopy ... /Log+:%Filename%


You can get the day, month and year using this cmd snippet:

for /f "usebackq tokens=1-3 delims=/" %%d in ('%date%') do (
    set day=%%d
    set month=%%e
    set year=%%f
)

(Warning: locale specific.)

Now you can write to different log files as you see fit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜