CMD File to delete .txt files that are at least 7 days old.


I need some help with a .cmd file that.  I haven't been able to find much useful info on this subject.  My .cmd file knowledge is limited to set,start, if,...  What I need is a .cmd file that will delete all .txt files in a given directory that are at least seven days old.  Can anyone help me with this?

The idea is something like the following...

for (each file in directory x)
  if (file type is .txt)
     if (file older than 7 days)
        delete the file;


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.



What is your OS and version? Is the "seven days" in your example
to be hard coded or will you want to pass a variable number of days
(and/or file attributes) to the script as a replaceable parameter?

Also, would a program made for that purpose work for you?
There are many available that are designed to delete by date.
Depending on the OS there aren't commands to delete by date
but a workaround script calling other commands can be made for it.

Otherwise I can recommend and provide a small utility that is made to
delete files in a specified location older than a given time or date.
It can be called from a cmd or batch file having a set period
or can be passed variable dates and/or file specifications.
Please post back with further details if interested and
I'll supply more info on it or indicate whether
you'd rather try to use commands
your OS already contains.
jwstairsAuthor Commented:
Thanks for the info.

I am running Windows NT 4.0. service pack 6.  To make the script reusable I would like to be able to pass the number of days and file type as parameters.  I would rather use native OS commands from a cmd file if that is possible.

Solution will follow after the coffee...

I need to solve the number of days for the month the file were created.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Check out the delold utility at

It looks like it will do what you want.

The CMD file command would look like the following.



The difficult part when using "native OS commands" is the math involved
to determine the date that would currently be seven (or whatever) days prior.
Especially because of the varying number of days in the months and the year,
the script could comprise a considerable amount of code just for that portion.
Then you'd still need to process a list of files having your specs for deleting,
and probably yet rely on external programs such as Find, Xcopy, FC, etc...
And if you want to recurse subdirectories during the search for specific files,
the script becomes even more complex. Other considerations include
converting variable date parameters to the user's system settings format
and error trapping for maltyped syntax, etc...

(That may be why Longbow is still drinking coffee, lol...)
Really I was waiting to see his script before offering examples
of others if you decide it's still how you prefer to do it.

Otherwise you might want to try the utility I mentioned...
It's name is Obsolete and you just run it like:

Obsolete [switches] Age FilePattern [dir]

It's a utility for deleting specified files that are older than a given time or date.
It accepts literal commands, such as; " Obsolete 7 Days *.TXT "
or  " Obsolete 4 Weeks *.TXT "  or  " Obsolete 2 Years *.TXT "  and so on...
You use an optional number with the names of the desired time period...
-i.e., seconds, minutes, hours, days, weeks, months, years.

Its default is to recursively search throughout entire directories and subdirectories.
It starts from the current working directory, or you can supply a drive and/or directory
from where you want to begin the search for files to delete.

Obsolete has an  -n  switch to have it do Nothing except display the names of files
that it would have deleted. It's handy for testing purposes.

There's an  -s  switch for Silent operation to eliminate the screen output
and switches for verification prompts, etc...

Note that switches used with Obsolete are case-sensitive.
The documentation with it is brief but explains all you need.

Here are some examples to delete files having a .TXT extension,
but you can substitute any valid wildcard or file spec...

To delete selected files regardless of their age,
use the "second" parameter, indicating files over 1 second old.
That would include all specified files, as they'll all certainly be at least that old.

To have Obsolete delete all files with a .TXT extension
in the current directory, you can do it like this:

Obsolete -n second *.TXT

The  -n  switch as used above tells Obsolete to only display the names
of files it finds meeting your criteria, but not delete anything it finds.
Remove the  -n  switch when you're ready to delete the files.
And again, these switches are case-sensitive.

To delete all designated files (*.TXT) that are 7 days old on the D: drive,
including all directories and subdirectories, specify the drive letter, like:

Obsolete 7 days *.TXT D:\

You can instruct Obsolete to prompt for verification before deleting files.
To enable the verification prompt, use the  -i+  switch, like:

Obsolete -i+ 7 days *.TXT D:\

To disable Obsolete's screen output during processing,
use its  -s  switch. To silently delete all .TXT files
in the C:\Temp directory and its subdirectories
that are 2 weeks old, do it like this:

Obsolete -s 2 weeks *.TXT C:\Temp

To do the same as above but only in the specified directory
and not its subdirectories, disable recursion
using the  -R-  switch, like:

Obsolete -s -R- 2 weeks *.TXT C:\Temp

To silently delete all .TXT files in the C:\Temp directory and its subdirectories
that are a day old, including Hidden, System and Read-Only files,
use the  -HWS  switches, like this:

Obsolete -s -HSW day *.TXT C:\Temp

So you could place a utility like Obsolete in a Path directory
and have a script to launch it with your desired switches.
The cmd file would have a line similar to:

Obsolete -s %1 %2 C:\Temp

...and you could call it when needed and pass your parameters like:

cmdfile 7 *.txt

Obsolete has other options you might find useful.
Documentation is included in the zip file. is small... (under 10k)

Using Obsolete is simpler than I make it appear... Hope it's legible.
Please post back if you want to use it, or something similar,
and we can maybe help with a cmd file if needed
for passing it parameters and launching.

If interested, you can download Obsolete from here:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jwstairsAuthor Commented:
I thought this would probably be the answer.  I gave it a try a couple of times myself and ran into many blocks....  

Thanks for the info everyone.  
I had no time to finish the job yesterday.

Here is the solution exactly as you asked : batch file.
There is only one day difference of 1 day for bissextil years.
This work in the same directory as the .txt files for now.

But it works great.

--- zDelTxt.bat ---
REM By Longbow on april 24 2002
@echo off

echo Deleted files >deleted.log
rem Check filedate (variables = _fdd _fmm _fyyyy)
for %%b in (*.txt) do call zCheck.bat %%b
del del.log
notepad deleted.log

--- zCheck.bat ----
REM By Longbow on april 24 2002
@echo off

dir %1 | find /I "/">c:\del.log

rem Check current date (variables = _dd _mm _yyyy)
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cyyyy=%%c
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cmm=%%b
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cdd=%%a
set _cyyyy=%yyyy:~2,2%
set _yyyy=

FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fyyyy=%%c
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fmm=%%b
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fdd=%%a

rem Number of days elapsed

If %_cmm%==01 set _cnd=0
If %_cmm%==02 set _cnd=31
If %_cmm%==03 set _cnd=59
If %_cmm%==04 set _cnd=90
If %_cmm%==05 set _cnd=120
If %_cmm%==06 set _cnd=151
If %_cmm%==07 set _cnd=181
If %_cmm%==08 set _cnd=212
If %_cmm%==09 set _cnd=243
If %_cmm%==10 set _cnd=273
If %_cmm%==11 set _cnd=304
If %_cmm%==12 set _cnd=334
If %_fmm%==01 set _fnd=0
If %_fmm%==02 set _fnd=31
If %_fmm%==03 set _fnd=59
If %_fmm%==04 set _fnd=90
If %_fmm%==05 set _fnd=120
If %_fmm%==06 set _fnd=151
If %_fmm%==07 set _fnd=181
If %_fmm%==08 set _fnd=212
If %_fmm%==09 set _fnd=243
If %_fmm%==10 set _fnd=273
If %_fmm%==11 set _fnd=304
If %_fmm%==12 set _fnd=334

set /A _timeF = %_fyyyy% - 1 * 365 + %_fnd% + %_fdd%
set /A _timeC = %_cyyyy% - 1 * 365 + %_cnd% + %_cdd%

set /A _timeE=_timeC-_timeF

rem If elapsed time is greater than 7 days log and delete the file
if %_timeE% GTR 7 goto DELETE
goto END

rem echo file to be deleted : %1
type del.log>>deleted.log
del %1

set _cdd=
set _cmm=
set _cyyyy=
set _fdd=
set _fmm=
set _fyyyy=
set _x=
set _y=
set _cnd=
set _fnd=
set _timeC=
set _timeF=
set _timeE=
I want to created automated scheduled script that would delete data older than a specific number of days from the current date from specific directories
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.