Page 1 of 1
How to write this script
Posted: 20 Apr 2014 20:40
by mdivk
Hello,
I have seven folders to be replicated. The seven folders are named as 20140428, 20140429, 20140430, 20140501, 20140502, 20140503, 20140504
All the three subfolders and their files in the seven folders are to be replicated into the rest weeks in 2014.
Can anyone help me with a script? Thank you very much.
Re: How to write this script
Posted: 20 Apr 2014 21:15
by foxidrive
How are they to be 'replicated'?
Do you merely want to copy the folders somewhere else?
Re: How to write this script
Posted: 21 Apr 2014 05:54
by mdivk
Thanks. I want to copy 20140428 to new folders with the same format for all Mondays in the year, new folders are named as 20140505 20140512 20140519 etc, and same to other week days ,
Re: How to write this script
Posted: 21 Apr 2014 06:32
by aGerman
Try
Code: Select all
@echo off &setlocal
:: initial variables
set "initialfolder=20140428"
set /a "yStart=%initialfolder:~,4%, y=yStart, m=100%initialfolder:~4,2% %% 100, d=100%initialfolder:~-2% %% 100"
:loop
call :DaysAdd %y% %m% %d% 1 y m d
if %y% equ %yStart% (
robocopy "%initialfolder%" "%y%%m%%d%" /mir
goto loop
)
pause
goto :eof
:DaysAdd ByVal_YearIn ByVal_MonthIn ByVal_DayIn ByVal_DeltaIn ByRef_YearOut ByRef_MonthOut ByRef_DayOut
setlocal EnableExtensions
set /a "Year = %~1, Month = 100%~2 %% 100, Day = 100%~3 %% 100"
set /a "a = 14 - Month, a /= 12, b = Year + 4800 - a, c = Month + 12 * a - 3, d = 153 * c + 2"
set /a "d = d / 5 + Day + b * 365 + b / 4 - b / 100 + b / 400 - 1 + %~4"
set /a "e = 4 * d + 3, e /= 146097, f = -e * 146097, f /= 4, f += d"
set /a "g = 4 * f + 3, g /= 1461, h = -1461 * g, h /= 4, h += f, i = 5 * h + 2, i /= 153, Day = 153 * i + 2, Day /= 5"
set /a "Day = -Day + h + 1, Month = -i / 10, Month *= 12, Month += i + 3, Year = e * 100 + g - 4800 + i / 10"
set /a "Month = 10%Month%, Day = 10%Day%"
endlocal &set "%~5=%Year%" &set "%~6=%Month:~-2%" &set "%~7=%Day:~-2%" &goto:eof
Regards
aGerman
EDIT Robocopy doesn't need any prior MD command.
Re: How to write this script
Posted: 21 Apr 2014 11:03
by mdivk
Thank you so much for the complex code, it's partially working:
The subfolders in 20140428 are copied to 20140505, however, the subfolders in 20140429 are not copied over to 20140506 (0428 were copied over), actually all the subfolders in new folders are from 20140428, how can the code be modified?
Thanks again for your help.
Re: How to write this script
Posted: 21 Apr 2014 11:10
by aGerman
Does it mean you have to take care of the week day?
Regards
aGerman
Re: How to write this script
Posted: 21 Apr 2014 12:15
by mdivk
All the contents in 20140428 are expected to be copied over into new folders: 20140505, 20140512, 20140519, etc
All the contents in 20140429 are expected to be copied over into new folders: 20140506, 20140513, 20140520, etc
All the contents in 20140430 are expected to be copied over into new folders: 20140507, 20140514, 20140521, etc
.....
Thank you again for your kind help.
Re: How to write this script
Posted: 21 Apr 2014 12:42
by aGerman
That's what I meant as well.
Code: Select all
@echo off &setlocal EnableDelayedExpansion
:: initial variables
set "initialfolder=20140428"
set /a "yStart=%initialfolder:~,4%, mStart=100%initialfolder:~4,2% %% 100, dStart=100%initialfolder:~-2% %% 100"
set /a "y=yStart, m=mStart, d=dStart, n=0"
:loop
call :DaysAdd %y% %m% %d% 7 y m d
if %y% equ %yStart% (
robocopy "%initialfolder%" "%y%%m%%d%" /mir
goto loop
)
set /a n += 1
if %n% lss 7 (
call :DaysAdd %yStart% %mStart% %dStart% %n% y m d
set "initialfolder=!y!!m!!d!"
goto loop
)
goto :eof
:DaysAdd ByVal_YearIn ByVal_MonthIn ByVal_DayIn ByVal_DeltaIn ByRef_YearOut ByRef_MonthOut ByRef_DayOut
setlocal EnableExtensions
set /a "Year = %~1, Month = 100%~2 %% 100, Day = 100%~3 %% 100"
set /a "a = 14 - Month, a /= 12, b = Year + 4800 - a, c = Month + 12 * a - 3, d = 153 * c + 2"
set /a "d = d / 5 + Day + b * 365 + b / 4 - b / 100 + b / 400 - 1 + %~4"
set /a "e = 4 * d + 3, e /= 146097, f = -e * 146097, f /= 4, f += d"
set /a "g = 4 * f + 3, g /= 1461, h = -1461 * g, h /= 4, h += f, i = 5 * h + 2, i /= 153, Day = 153 * i + 2, Day /= 5"
set /a "Day = -Day + h + 1, Month = -i / 10, Month *= 12, Month += i + 3, Year = e * 100 + g - 4800 + i / 10"
set /a "Month = 10%Month%, Day = 10%Day%"
endlocal &set "%~5=%Year%" &set "%~6=%Month:~-2%" &set "%~7=%Day:~-2%" &goto:eof
Make sure the folders for 7 days ("initialfolder" onwards) do already exist.
Regards
aGerman
Re: How to write this script
Posted: 21 Apr 2014 13:14
by mdivk
Thank you so much for your help, your code is beautifully working here.