Help with Robocopy script


I have a Windows 2003 server where I have an application that logs daily files. The folder is C:\Logs, and each day, a folder with the date is created and, within this, a log file with the date too.

For example:


This process continues. So, after a week, I would have folders such as:


etc, each with their own log file within.

I would like to copy each day's log file to another server and was going to use Robocopy via a scheduled task. Basically, each day, I would want to upload the previous day's log file. But I'm not sure how to upload the previous day's log file only, and leave the rest alone? Does anyone know what sort of switches I could use?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

/MAXAGE:n :: MAXimum file AGE – exclude files older than n days/date.

If you want only the last days files:

Robocopy   /MAXAGE:1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
A vbscript or Batch file could help load the data, but XCopy or even XXCopy might be better...

For the best flexibility check out xxcopy
chuckp2010Author Commented:
Thanks both....I'll check out xxcopy.

/MAXAGE:n :: MAXimum file AGE – exclude files older than n days/date.

If you want only the last days files:

Robocopy <source> <destination> /MAXAGE:1"

Let's say that I set that up as a scheduled task today (Wed). Remember that I want to upload the -previous- day's files every day, i.e. on a Thursday, I want to upload Wednesday's files etc.

So I set up the scheduled Robocopy command above to run at 01:00 every day. So on Thursday morning at 01:00, I want Wednesday's log file copied over.

But - because I specifying to exclude folders older than one day, am I not going to upload Thursday's file (i.e. a file that has an age of 0)?

Hope my q makes sense :)
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

So there is both a Wed and Thurs file, but you only want to grab Wed?
If that's the case then you would need to play with /MINAGE as well
I believe that you can set minage to 0 and it will exclude the last 24 hours, but you'd need to play with it to get your timestamps the way you want them.
Yes, makes sense!

You should be able to also use the /minage:1 switch to say the folder/ file needs to be at a minimum 1 day old.
chuckp2010Author Commented:
Hmm, still not working. The problem is that for yesterday, the log could have been written to last at any point in that 24 hour period, depending on the application's activity.

Is there anyway with Robocopy to just say - "if the file was written to at any point yesterday, then copy it to xx"
chuckp2010Author Commented:

From testing, this seems to work:

robocopy \\server\c$\logs\\ \\server2\c$\copied\\  /maxage:2 /minage:1 /s

I just want to be sure though :) The previous day's log file can be written to any time in yesterday's 24 hour period, depending on the app usage, so would maxage:2 and minage:1 cover all bases?
I just tested it:
MAXAGE:2 will give you the previous 24 hours (i.e. the 24 hours prior to the current 24 hour period)
MINAGE:1 will skip anything from the current 24 hours
chuckp2010Author Commented:
Thanks..but when you say 24 period, do you mean "day"?

So say I wanted the script to run at 01:00 every day.

On Thursday 01:00, the script runs with /maxage:2 and minage:1

The last time the Wed log file was updated was Wed 22:00

Would Wed's file still be copied over?
If you know the exact folder name that you want to copy  - which you do, it';s yesterdays date in yyyymmdd format - you could build a string variable in a batch file to consist of the year, month and day; then Robocopy from the folder of that name, without switches.

I've used a similar code snippet for a while in a similar situiation, creating folders for a specific day's backup.   The batch file code is a bit obscure, but seems to work OK.  Note that the details are sensitive to the date format that is configured on your system

Where I got it:

REM to test your date format - see what order things come out
For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set CDate=%%a-%%b-%%c-%%d-%%e
@echo testdate = %CDate%

rem Once you know the order, Combine them in whatever way suits you - this works on mine but may be 

different on yours
For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set CDate=%%c%%b%%a
@echo date = %CDate%

md \\newserver\logs\%Cdate%
robocopy c:\logs\%Cdate%  \\newserver\logs\%Cdate%

Open in new window

note - some line breaks have been corrupted in the code I posted: LINE 7 should be attached to the end of line 5.  You can lose all the testing and ECHO commands after you know it iis working.
It's a rolling 24 hour period, so no, a file that was timestamped with 22:00 Wed would not get copied on Thurs 01:00 task as it's only 3 hours old.
chuckp2010Author Commented:
Thanks both -

Sheshiro - if I ran the task on Friday at 00:00, then I guess that would cover it? The last 24 hours would exclude all of Thursday, and the previous 24 hours would include all of Wednesday?

aflockhart - thanks, I will check that out!
"Sheshiro - if I ran the task on Friday at 00:00, then I guess that would cover it? The last 24 hours would exclude all of Thursday, and the previous 24 hours would include all of Wednesday?"
- Correct, the wed 22:00 file is 26 hours old and would be picked up on Friday 00:00
chuckp2010Author Commented:
Thanks Sheshiro.

Can you think of anyway that I can perform the same objective (i.e copy over the previous day's [Wed] file only), but have the Robocopy run at some point on the next day (Thurs) rather than Friday 00:00?
If the file stops getting written to @ 22:00 Wed, then setting the robocopy to run @ 22:15 Thursday would do it.

Other than that, I'm sure there's a For loop that could do it... but I'm not a codemonkey :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.