Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2002-04-22
Medium Priority
Last Modified: 2013-11-18

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;


Question by:jwstairs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 6961182


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.

Author Comment

ID: 6962098
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.

LVL 10

Expert Comment

ID: 6962994
Solution will follow after the coffee...

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 6963958
Check out the delold utility at http://www.michna.com/software.htm#DelOld

It looks like it will do what you want.

The CMD file command would look like the following.


Accepted Solution

rin1010 earned 900 total points
ID: 6964714


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.
Obsolete.com 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:



Author Comment

ID: 6964819
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.  
LVL 10

Expert Comment

ID: 6965205
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=

Expert Comment

ID: 29884631
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question