|
Answer» Dear all, I'm trying to write a batch to remove a subfolder with file of 8 days old.
However, I realised whenever ":ChangeMonth" is invoked, it doesn't run properly.
Example: I set system date to 9 Dec 2005, it runs properly (delete file-20051202 in C:\Test). But when I set system date to 7 Dec 2005, it didn't delete file-20051130 in C:\Test.
Pls kindly help.. The following is a SAMPLE of my codes. Your help will be greatly APPRECIATED. Thank you so much.
SET DayS=%date:~7,2%
:: strip leading zero from Day IF %date:~7,1%==0 SET DayS=%date:~8,1%
SET YY=%date:~10,4%
:: if day is in 1st-7th of month, invoke ChangeMonth IF %DayS:~0% LSS 8 CALL :ChangeMonth IF NOT %DayS:~0% LSS 8 CALL :SameMonth
:: add leading zero to variable DD if it is less than 10 IF %DD:~0% LSS 10 SET DD=0%DD:~0%
:: set date of 1 week back to be in YYYYMMDD format SET SortDate=%YY:~0%%MM:~0%%DD:~0%
:: remove directory and file IF Exist C:\Test\file-%SortDate% (rd /S /Q C:\Test\file-%SortDate%)
:SameMonth SET /A DD=%DayS:~0%-7 SET MM=%date:~4,2% GOTO:EOF
:ChangeMonth IF %date:~4,2%==01 ( SET /A DD=31-(7-%DayS:~0%) SET MM=12 SET /A YY=%YY:~0%-1 )
... etc
IF %date:~4,2%==12 ( SET /A DD=30-(7-%DayS:~0%) IF %date:~4,2%==12 SET MM=11 ) GOTO:EOFQuotes are needed when you use grouping on the SET statement.
Code: [Select] IF %date:~4,2%==01 ( SET /A DD="31-(7-%DayS:~0%)" SET MM=12 SET /A YY=%YY:~0%-1 )
Try using concatenation to replace the block IF's:
Code: [Select] IF %date:~4,2%==01 (SET /A DD="31-(7-%DayS:~0%)" & SET MM=12 & SET /A YY=%YY:~0%-1)
Also, why the nested IF for December? Should not December be like any other month?
Code: [Select] IF %date:~4,2%==12 (SET /A DD="30-(7-%DayS:~0%)" & SET MM=11)
From your use of batch language, I'm guessing you have either XP or Win2K. Next time consider using a Scripting SOLUTION. You'll find it a better tool for the job.
Good luck. Thanks alot Sidewinder. It works correctly now. Yup, the nested IF statement is redundant. I was doing some testing here and there and I FORGOT to remove it. Thanks so much for your help! =D
|