Solved

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

Posted on 2002-04-22
8
13,775 Views
Last Modified: 2013-11-18
Hi,

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;
  }
}

Thanks,

Jeff
0
Comment
Question by:jwstairs
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 7

Expert Comment

by:rin1010
ID: 6961182

Jeff,

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.
 
0
 

Author Comment

by:jwstairs
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.

Regards,
Jeff
0
 
LVL 10

Expert Comment

by:Longbow
ID: 6962994
Solution will follow after the coffee...
;-)

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

Expert Comment

by:Jason_S
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.

DELOLD C:\TEMP\*.TMP 7
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Accepted Solution

by:
rin1010 earned 300 total points
ID: 6964714

Jeff,

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:

http://users.aol.com/pasacaca/obsolete/obsolete.zip

 
0
 

Author Comment

by:jwstairs
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.  
0
 
LVL 10

Expert Comment

by:Longbow
ID: 6965205
Sorry
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
echo.>del.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
echo.>>deleted.log
if %_timeE% GTR 7 goto DELETE
goto END

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

:END
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=
-------------------
0
 

Expert Comment

by:Aariz
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now