Delete all files in tree over 7 days old

Sulcata used Ask the Experts™
I need a script or line that can be added to this one to delete all folders and files created by the following script that are older than 7 days old. Will be run from XP,2000 and 98se.

REM This backs up the H:\ drive at log off
REM Place in user_config/windows_settings/scripts/logoff
REM This section was found at

REM ----------------- this creates a date string

for /f "tokens=2-4 delims=/ " %%a in ('Date /T') do set DateVar1=d%%c%%a%%b
for /f "tokens=1-3 delims=: " %%a in ('TIME /T') do set TimeVar1=t%%a%%b%%c%
SET DateVar=%DateVar1%\%TimeVar1%

MD c:\DailyLocal\%VarVar%

REM ----------------- this determines if the directory exists before creating one
IF NOT EXIST C:\DailyLocal\%DateVar%\NUL MD C:\DailyLocal\%DateVar%

REM copies file from and to directory
xcopy /E /Y /Q /k /I H\:*.* c:\DailyLocal\%DateVar%
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
dbruntonQuid, Me Anxius Sum?  Illegitimi non carborundum.

This link talks about what you want

There are links to utils for date deleting and also a batch file that does that as well


You say it will be "run from XP, 2000 and 98se."
Some of the commands in the examples you posted
are specific to the OS and command interpreter being used
and the reference to the Nul device may not always be available, etc...

Expert SteveGTR has posted some recent solutions
for manipulating files based on date under NT, 2K, and XP
and I'll provide the urls or paste the examples if you're interested...

But for a solution to work under all the operating systems mentioned
you may want to consider using a utility made for that purpose...

Here's some portions edited from my comments in that post
including urls where you can get a couple of utilities
for deleting files based on date and time...

If you just want to use an external program to do it, I can recommend a couple...
I've placed them on a server where you can download and test them if needed.
They'll both delete files by date or other specified parameters using wildcards
and your file specs and will recursively work on subdirectories if necessary.

These programs work well, are freeware and include documentation and examples.
One is named "Delete Enhanced" and the other is named "Obsolete."
The former comes in a 16-bit version and a 32-bit version...

Here are the urls of the deleting utilities:

 "Delete Enhanced" file deleter 16-bit :

 "Delete Enhanced" file deleter 32-bit :

 "Obsolete" file deleter :


If the XCopy routine is working as you want it
then you may be able to use its /D switch
together with your obtained date string
and then delete the remaining files
after the copy process is done.

Please post back if none of this helps
or if you want other references
or batch file examples...


rin1010, you are right about the variables not working under win98. We are 3 wks away from upgrading those systems so I am not going to worry about them to much.
Thank you for the programs they work great!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial