Home > Tips > How to delete files older than certain Days

How to delete files older than certain Days

Its general requirement and I have seen many people asking this question, “How to delete files older than given no. of days”. This can be done via various methods. If you are scripting geek, then it’s pretty easy task for you. If you are lazy/hate at scripts, then I have command for you(look at the command at bottom and use at your own risk :-) ).

Download the FORFILES tool from Resource Kit and frame your command. You can play around with FORFILES options to get required functionality.

Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-d<+|-><DDMMYYYY|DD>] [-s]

 -pPath               Path where to start searching
 -mSearch Mask        Search files according to <Search Mask>
 -cCommand            Command to execute on each file(s)
 -d<+|-><DDMMYYYY|DD> Select files with date >= or <=DDMMYYYY (UTC)
                      or files having date >= or <= (current date – DD days)
 -s                   Recurse directories
 -v                   Verbose mode

The following variables can be used in Command :
 @FILE, @FNAME_WITHOUT_EXT, @EXT, @PATH, @RELPATH, @ISDIR, @FSIZE, @FDATE,
 @FTIME

To include special hexa characters in the command line : use 0xHH

Default : <Directory : .> <Search Mask : *.*>  <Command : “CMD /C Echo @FILE”>
Examples :
FORFILES -pc: -s -m*.BAT -c”CMD /C Echo @FILE is a batch file”
FORFILES -pc: -s -m*.* -c”CMD /C if @ISDIR==TRUE echo @FILE is a directory”
FORFILES -pc: -s -m*.* -d-100 -c”CMD /C Echo @FILE : date >= 100 days”
FORFILES -pc: -s -m*.* -d-01011993 -c”CMD /C Echo @FILE is quite old!”
FORFILES -pc: -s -m*.* -c”CMD /c echo extension of @FILE is 0×22@EXT0x22

Example:  Forfiles -p “<path>” -s -m *.* -d -10 -c “Cmd /C del @FILE”

Hope this is helpful.

Happy Learning..,
Sitaram Pamarthi

Categories: Tips
  1. March 30, 2010 at 6:49 pm | #1
  2. Rod Seibert
    September 29, 2010 at 11:05 pm | #2

    Reading your article really helped me with my problem. I want to thank you for writing this article.

  3. October 19, 2010 at 8:12 pm | #3

    Glad that it helped you…

  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>