Deleting older than 7 day directories

Microsoft Windows xp professional editio...
October 11, 2009 at 03:41:00
Specs: Windows XP
Hi Guys
I have a batch script which creates directories and then copy my files to it every day.
How can I adjust this script to delete the older than 7 days directories
The script I use is as follow:
for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var=%%A%%B%%C
md "c:\backup\%var%"
cd test
xcopy *.* c:\backup\%var%

Thanking you in regards

See More: Deleting older than 7 day directories

Report •

October 11, 2009 at 16:38:27
See if something like this will work for you.

@echo off
SETLOCAL EnableDelayedExpansion
set NOW=%date:~6,4%-%date:~0,2%-%date:~3,2%
md "C:\backup\%now%"
xcopy "C:\test\*.*" "C:\backup\%now%"
for /f "Tokens=*" %%a in ('dir /b /ad "C:\backup"') do (
Set Folder=%%a
set /a diff=%Now:~8,2%-!Folder:~8,2!
if !diff! gtr 7 rd /s /q C:\Backup\!Folder!
echo !diff!

Report •

October 11, 2009 at 19:11:00
Okay, after I posted the above I started thinking, this will work great as long as it's not the first seven days of the month. So I must say I think I've got it all figured out except you may have a problem with febuarary in leap years. This is what I've came up with.

@echo off
set day=86400
set /a year=day*365
set /a strip=day*7
set Today=%date:~6,4%-%date:~0,2%-%date:~3,2%
md "C:\backup\%Today%"
xcopy "C:\test\*.*" "C:\backup\%Today%"

call :epoch %date%
set /a slice=epoch-strip
for /f "delims=" %%f in ('dir /ad /b "C:\Backup"') do (
call :epoch %%~tf
if !epoch! LEQ %slice% rd /s /q "C:\Backup\%%f"
exit /b 0

for /f "tokens=1,2,3 delims=/" %%d in ('echo %1') do (
set Years=%%f
set Months=%%d
set Days=%%e
if "!Months:~0,1!"=="0" set Months=!Months:~1,1!
if "!Days:~0,1!"=="0" set Days=!Days:~1,1!
set /a Days=Days*day
set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do (
if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1)
set Months=!_months!
set /a Years=(Years-1970)*year
set /a Epoch=Years+Months+Days
endlocal& set Epoch=%Epoch%
exit /b 0

Report •

October 11, 2009 at 21:48:56
hello, i once made a formula in QB for generating julian date from month, day, year. it was
JD = (MM - 1) * 31 - INT(MM / 2.31) - (1 - CINT(1 / MM + .01)) * INT((YY MOD 4) / ((YY MOD 4) - .01) + 1) + DA
i also had a javascript version somewhere, i'll try to dig it up.
the formula attempts to account for leap years and february's.
The constant "2.31" was arrived at using successive
approximation (I'm not a mathematician). if this is doable
in XP batch scripting, it might save some coding.

Report •

Related Solutions

October 11, 2009 at 23:13:25
Hi Guys
Thanks for the help. Gtaion, your script deleted all my directories which I had as test. luckily I did not run it on the server as a life script ;-) Do you know what went wrong?
It now tells me : File creation error - Cannot create a file when that file already exists (My directory is empty, so I guess its referring to my c:\backup directory.) Below that error I get another error: Unable to create directory C:\backup\0\21-20-9\
0 Files coppied.
Please note that I was experimenting with advanced dates and was creating a directory for each day from the 11th till 21st. My calendar is standing on 21 of October at the moment, that explains the 21 in the above error. I will turn my date back now for the correct date ;-)
What do you suggest regarding the script?

Member: nbrane, I am trying to do this in a batch script, and will need it in this as my client does not run a web server ;-)

Thank you for all the comments

Report •

October 12, 2009 at 01:25:55
There is a good date difference script here (datdiff), it may be a little picky about the date format but could be modified to suit your needs. You would probably also need to change the output a little.

Batch Variable how to

Report •

October 12, 2009 at 02:05:15
I wrote a quick script based on the date converter I linked
to above, you may need to change several things depending on your
date output.

The :jdate subroutine is called twice, but I'm only guessing at
which elements to pass it as the date is dependent on your
regional settings, Jdate should be called with "YYYY MM DD".

I have also prefixed the rd /s command with echo so it doesn't
actually remove the directories:


for /F "tokens=2-4 delims=/- " %%A in ('date/T') do (
    set var=%%A%%B%%C
    call :jdate ???????????
    rem fill in question marks with YYYY MM DD
set today=%jdate%
md "c:\backup\%var%"
cd test
xcopy *.* c:\backup\%var%
for /d %%a in ("c:\backup\*") do (
    for /f "tokens=1-3 delims=/- " %%b in ("%~ta") do (
        call :jdate ???????
        rem fill in question marks with YYYY MM DD
        rem %~ta will likely have a different format that date /t
        call :sevendays "%%~a"
goto :eof

set /a diff=today-jdate
if %dif% gtr 7 echo rd /s "%~1"
goto :eof

::this subroutine was... 
::Written by Rob van der Woude
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
IF 1%MM% LSS 110 SET MM=%MM:~1%
IF 1%DD% LSS 110 SET DD=%DD:~1%
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (,
:: and converted to batch code by Ron Bakowski.
SET /A Month1 = ( %MM% - 14 ) / 12
SET /A Year1  = %1 + 4800
SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
FOR %%A IN (Month1 Year1) DO SET %%A=

Batch Variable how to

Report •

October 12, 2009 at 08:21:57
Techwise, you're right there is something wrong with response #2 and I can't figure it out right now. I was just thinking what if we make it way easier and use the forfiles command. It should work if you have anything winNT or newer. Although if you are on a NT or 2000 machine the syntax is a little different then the following.
FYI: until you get some folders that are older then 7 days, it will display an error stating no files found meeting the search criteria. That is okay. It's just saying that you don't have any folders older then 7 days. Also we dont have the same regional settings which was giving you the file creation error. based on what you posted on the creation error, I think I adjusted for your regional settings (which I'm thinking your date format is YYYY/MM/DD)

@echo off
set Today=%date:~0,4%-%date:~5,2%-%date:~8,2%
md "C:\backup\%Today%"
xcopy "C:\test\*.*" "C:\backup\%Today%"
forfiles /p C:\backup /d -7 /c "Cmd /c RD /s /q @file"

Report •

October 12, 2009 at 10:02:04
As much as I hate to push other languages, VBScript is much better equipped to handle this.
For Each f In CreateObject("Scripting.FileSystemObject").GetFolder(".").SubFolders
  If f.DateCreated < (Date - 7) Then f.Delete True
Next 'f

Report •

October 12, 2009 at 22:28:39
Hi Guys,
I found something which bothers me.
I am testing on my windows xp home edition, and I installed the copy script to my clients xp pro machine just to give him some kind of solution. I found that my xp home edition displayed the date completely different (like 1011 - representing 11 Oct) and the xp pro machine displaying the full date (10112009). Why is it so different? I thought that these commands wouldn't be so different in the different versions. I will now need to install the script on my xp pro box to be sure that the solution works. I will look and experiment with your replies tonight and will give you feedback on it ;-) Thanks guys. This forum is great stuff! ;-)

Report •

October 12, 2009 at 22:40:51
The time and date formatting is dependent (more or less) on the machine's localization settings. (Found in "Regional and Language Options".) This is another reason to pick a language that is not Batch Script.

Report •

October 13, 2009 at 15:23:56
I have to agree, I always forget about the regional settings being able to change the way that the date variable is displayed. So if you are going to do something VBScript may I suggest the following? I set the Destination directory according to your origanal post to "C:\Backup". Then not knowing your source directory I figured why not set it as the directory that the script is residing in? Obviously you could change that to reflect the actual source directory, that way you could maybe set it in the User's logon scripts in group policy or maybe just put it on the desktop for the user to run.
It doesn't take any special permissions to run a VBScript so if a user could do it with a Batch file they would be able to use a VBScript. The only thing is they wont get a black window to pop up for a second.

Otherwise I believe the Following is what you were looking for, Just copy into a new text like you would a batch file, except make the File extension .VBS instead of .BAT.

Option Explicit

Dim YYYYMMDD, objFSO, strSourceDir, strDestDir, strNewFolder, Folder

YYYYMMDD = ((Year(Date))&Right("0" & Month(Date()),2)_
&Right("0" & Day(Date()),2))

strSourceDir = CreateObject("Wscript.Shell").CurrentDirectory
strDestDir = "C:\Backup"
strNewFolder = StrDestDir & "\" & YYYYMMDD

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(strDestDir) Then
End If

If Not objFSO.FolderExists(strNewFolder) Then
End If

objFSO.CopyFolder strSourceDir, strNewFolder, True

For Each Folder In objFSO.GetFolder(strDestDir).SubFolders
If Folder.DateCreated < (Date - 7) Then Folder.Delete True

Report •

Ask Question