delete file older that 3 days

V

VERSCHUEREN Geert

Hello,

is there a way to delete the files older than 3 days in a specific path by
just using msdos(/CLI) batch scripting and without using forfiles.exe from
MS?
 
J

Jerold Schulman

Hello,

is there a way to delete the files older than 3 days in a specific path by
just using msdos(/CLI) batch scripting and without using forfiles.exe from
MS?
See tip 274 in the 'Tips & Tricks' at http://www.jsiinc.com

If you need to delete in all the subdirectories also:

set folder=c:\foldername
for /f "Target=*" %%d in ('dir %folder% /s /b /ad') do delold "%%d\*.*" 3

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
R

Ritchie

VERSCHUEREN Geert said:
is there a way to delete the files older than 3 days in a specific path by
just using msdos(/CLI) batch scripting and without using forfiles.exe from

Using 'pure' batch, yes, there's a couple of ways. I'll briefly describe the less
inefficient method.

First determine and load the current date components into variables, then
convert the date into the number of elapsed days since date x. Then subtract
the required number of days (three in your case) and convert the result back
into a date. Combine the resulting date components into one variable in the
format of yyyymmdd or yyyymmddhhmm (depends on the resolution you require,
for hhmm, you might use 0000).

See this query for examples on obtaining the current date:-
http://groups.google.com/groups?q=batch.nt OR%
20cmdprompt.admin+ritchie+getdate+gettime

See this query for examples of converting a date to a number and vice-versa:-
http://groups.google.com/groups?q=batch.nt OR cmdprompt.admin+ritchie+datetodays+daystodate

Next use a FOR /F loop to parse the output from the DIR command which is used
to enumerate the directory of interest. Each 'record' is then parsed again by
a second (inner) FOR /F loop to extract the file's timestamp components. This
loop then CALLs a routine passing the date/time info and filename.

The called routine can now perform a _string_ comparison using the IF
command in conjuction with the extended comparison operators (GTR, GEQ etc)
and if true, perform the required operation on the file (i.e. delete the file).

If the final script is to run on a single machine, it's really quite
straightforward. If it will be used on two or more machine it gets slightly
more complex, requiring one of two extra FOR /F to 'fix' the date and take
into account different O/S - but perfectly do-able.

Post again for further info.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top