Solved

Help with Robocopy script

Posted on 2010-08-18
16
838 Views
Last Modified: 2012-05-10
Hi

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:

C:\Logs\20100812\Log_20100812.txt

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

C:\Logs\20100813
C:\Logs\20100814
C:\Logs\20100815

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?
0
Comment
Question by:chuckp2010
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 1

Accepted Solution

by:
Sheshiro earned 250 total points
Comment Utility
/MAXAGE:n :: MAXimum file AGE – exclude files older than n days/date.

If you want only the last days files:

Robocopy   /MAXAGE:1

0
 
LVL 14

Assisted Solution

by:athomsfere
athomsfere earned 250 total points
Comment Utility
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
http://www.xxcopy.com/xxcopy17.htm
0
 

Author Comment

by:chuckp2010
Comment Utility
Thanks both....I'll check out xxcopy.

Regarding:
"
/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 :)
0
 
LVL 1

Expert Comment

by:Sheshiro
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:athomsfere
Comment Utility
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.
0
 

Author Comment

by:chuckp2010
Comment Utility
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"
0
 

Author Comment

by:chuckp2010
Comment Utility
Actually

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?
0
 
LVL 1

Expert Comment

by:Sheshiro
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:chuckp2010
Comment Utility
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?
0
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
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:  http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/


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

0
 
LVL 17

Expert Comment

by:aflockhart
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:Sheshiro
Comment Utility
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.
0
 

Author Comment

by:chuckp2010
Comment Utility
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!
0
 
LVL 1

Expert Comment

by:Sheshiro
Comment Utility
"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
0
 

Author Comment

by:chuckp2010
Comment Utility
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?
0
 
LVL 1

Expert Comment

by:Sheshiro
Comment Utility
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 :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now