Content

Delete Files Older Than Date Using Batch Files

13 Apr

+ 7

This problem has nagged at me for years.  Here is a batch command to delete files on a Windows 2003 machine.

Forfiles -pC:\backup -s -m*.* -d-5 -c "cmd /c del /q @path"

This will delete all files in my backup directory older than 5 days. To test it first, use this:

Forfiles -pC:\backup -s -m*.* -d-5 -c "cmd /C Echo 0x22@Path\@File0x22"

This will print out each file that you will be deleting.

Now playing: Lamb Of GodTerror And Hubris In The House Of Frank Pollard

Thanks for that. Forfiles FTW!

Just what I needed to clean up a load of cookies folders without wiping them out entirely. A nice little command.

Very useful; forfiles.

However, shouldn’t the first line read:

forfiles -pC:\backup -s -m*.* -d-5 -c “cmd /c del /q @PATH\@FILE”

…or all files would be deleted in the @PATH not the list of files over 5 days old such as @PATH/@FILE would result in?

Try this:
forfiles -pC:\backup -s -m*.* -d-5 -c “cmd /c echo del /q @PATH”

…echos a list of delete commands with the same (duplicate) path string (with no filename) for each file found in the directory over 5 days old) that was older than 5 days.

You Get This (duplicate directory only names, not desired):
del -q c:\backup\db2\
del -q c:\backup\db2\
del -q c:\backup\mssql\
del -q c:\backup\mssql\
del -q c:\backup\mssql\
del -q c:\backup\oracle\
del -q c:\backup\oracle\

NOTE: these (above) del commands would delete all files in these directories, not just the one over 5 days old

VS. this:
forfiles -pC:\backup -s -m*.* -d-5 -c “cmd /c echo del /q @PATH/@FILE”

You get This (desired):
del -q c:\backup\db2\file_01.zip
del -q c:\backup\db2\file_02.zip
del -q c:\backup\mssql\file_51.zip
del -q c:\backup\mssql\file_52.zip
del -q c:\backup\mssql\file_53.zip
del -q c:\backup\oracle\file_71.zip
del -q c:\backup\oracle\file_72.zip

NOTE: these (above, desired) del commands would the specifc files that are over 5 days old

Or am I missing something?

Also, the version of forfiles that I got requires @PATH, @FILE, etc. to be ALL uppercase (maybe my version?).

BTW.. thank you for turning me on to forfiles. :-) does the job and is more strait forward than find|xargs.

–marc

Not sure what version of forfiles you have – but my Windows 2000 version worked as I wrote it. Maybe yours was Windows 2003?

maybe… below is the help screen fro the forfiles that I am using. It clearly shows @FILE and @PATH as 2 distinct variables. The all-uppercase requirement is not so clear but verified during testing. I just brought it up because the delete example forfiles syntax did not match your test case example forfiles syntax (i.e. @PATH only vs. @PATH/@FILE) and I did not want anyone to delete more than they bargained for.

File Stats:
-rwxrwxrwx 1 user group 12048 Sep 3 00:28 forfiles.exe

Help Screen:
FORFILES v 1.1 – emmanubo@microsoft.com – 4/98

Syntax : FORFILES [-pPath] [-mSearch Mask] [-ccommand] [-d] [-s]

-pPath Path where to start searching
-mSearch Mask Search files according to
-cCommand Command to execute on each file(s)
-d Select files with date >= or = 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 :
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@EXT0×22″

Thanks again.
–marc

Hey Scott,

thanks for the information. I was also searching for this. Got a couple of backup things that need special care. So this commandline tool came in handy.

regards from holland

Henk

Thanks a lot !!
very useful.

Comment

#

I reserve the right to delete and / or modify the comments that contain inappropriate language, spam or other behavior not appropriate in a civilized community. If your comment does not appear, it may be that akismet caught it.