|
Answer» I'm using a batch file to backup data from a PC to a server. The batch file is using Robocopy as the command to copy the folders/files. I have a little problem.
The SOURCE directory will contain folders in the format of date i.E., "dd-mm-yyyy" , so for today's date, 9th October 2007 there will be a folder whose name is - "09_10_2007" along with folders for earlier dates like "08_10_2007" and so on.
The purpose is, for any given date I'm supposed to copy the folder for the previous date (or a folder that has a name with a 2days old date)and thats what I cant achieve. On 9th Oct 07 I can copy the folder "09_10_2007" but on 9th Oct 07 I need to copy the folder "08_10_2007"
Using the following command I can copy the folder for current date :
robocopy "\\xxx.xxx.xxx.xxx\C$\Backup\%Date:~-7,2%_%Date:~-10,2%_%Date:~-4,4%" "%BackTemp%" /e /ZB /R:2 /w:10 /log+:""%BACKLOG%"\logs%Date:~-7,2%_%Date:~-10,2%_%Date:~-4,4%.txt"
Where "%BackTemp%" & "%BackLog%" are pre-defined variables.
Can anyone please advice me how I can copy the folder for the previous date on any given date ??You need an algorithm to determine yesterday's date... Many lines of code... Perl can do it in ONE line:-
Google for "yesterday's date batch"
Thanks contrex. I had been Googling uot for ACHIEVING this , I did find something useful but would need your assistance on this to pickup a folder with yesterday's date from the source ( as I had described earlier)1 calculate yesterday's date in the right format: dd-mm-yyyy (similar to your existing today's date format) 2 Put it in a variable called e.g. YDate 3 change each occurence of Date to YDate in your Robocopy command, and it will get sliced just like todays's date string would.
e.g. Robocopy "\\xxx.xxx.xxx.xxx\C$\Backup\%YDate:~-7,2%...(etc)
generalised algorithm for calculating yesterdays date:
date is dd-mm-yyyy
(easiest case) If the day number dd is 2 or greater
keep mm and yyyy the same subtract 1 from dd
(harder ones) If the day number dd is 1
if the mm is 5,7,10 or 12 keep yyyy the same subtract 1 from mm make dd=30
if the mm is 2,4,6,8,9 or 11 keep yyyy the same subtract 1 from mm make dd=31
(hardest, often forgotten or done wrongly) if the mm is 3 if yyyy is not a leap year keep yyyy the same subtract 1 from mm make dd=28 if yyyy is a leap year keep yyyy the same subtract 1 from mm make dd=29
if the mm is 1 if the day is 2 or more keep mm and yyyy the same subtract 1 from dd if the day is 1 subtract 1 from yyyy make mm=12 make dd=31
the next closest thing you can use natively is vbscript here's one that calculate yesterday's date for you in the format dd_mth_year Code: [Select]d=Now yesterDay=DateAdd("d",-1,d) yestDay= Day(yesterDay) yestMth= Month(yesterDay) yestYr=Year(yesterDay) WScript.Echo yestDay&"_"&yestMth&"_"&yestYr save it as somescript.vbs
usage in batch:
Code: [Select]for /F %%a in ('cscript /nologo somescript.vbs') do set yest=%%a then you can use the variable %yest% anywhere in your batch
|