Content

Delete Files Older Than Date Using Batch Files

13 Apr

+ 19

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@EXT0x22″

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.

Very nice, useful, timely info. For more honed seraching of files, one can also use a simple scripting language such as biterscripting. There are some sample scripts posted at http://www.biterscripting.com/samples_computer.html that have samples of searching and auto editing of files on a computer.

Nice little helpfull hint. Saved me some time. Thanks from Texas!

Hey, I did this command but it doesn’t actually delete the file. It just outputs the command to delete the file. I am a little confused on how to actually delete the file?

forfiles /p c:\test -s -m *.* -d -07 -c “cmd /c echo del /q @path@file”

Then it just displays

del /q “c:\test\filename.doc”

But it doesn’t delete the filename

Works great and just what I needed, but as with marc above,my forfiles (on Win2003 Server) did not work correctly with your syntax. Had to add spaces in some places and change the delete syntax to *.??? to get mine to work correctly.

One thing to consider..with this batch, if no ‘new’ backup is done within five days from the date on your computer, all your backups get deleted. Not a problem most of the time, but say your accounting software is backed up manually…and the right people are out sick for a week…just food for thought. Some might want to allow for a longer time span (say 15 days) to make certain that the last backup is never deleted. If it happens anyway, check out r-undelete. You can google it. Thanks again from Texas.

Thanks for this.
One thing I noticed is that it does not exclude the subfolders even with a command of:
forfiles -pC:\folder\ -m*.* -d-5 -c”cmd /c echo del /q @PATH/@FILE”

In other words, if you have a subfolder of
“C:\folder\my_folder” then the “my_folder” WILL be deleted as well….so what is the point of “-s” parameter?
Thanks!

is this running in winxp?
can i download forfiles in winxp?

Thank you, this batch file is brilliant. I’ve been playing with DOS scripting for years and never came across the ForFiles cmd. WEIRD! Thanks, this is a great article!

-Ted

I have the same results as BigPimpin. It successfully echoes back the DEL commands for each file, but does not actually delete the file. Any ideas about this ? I am logged in as domain admin and testing interactively. I have not scripted and scheduled this yet.

forfiles /p D:\Backups\ /S /M *.bak /D -1 /C “cmd
/c echo del /q @PATH”

Per http://winhlp.com/node/180, I downloaded a differnet version of forfiles that uses Unix switches with a dash (-), instead of Windows switches with a slash (/).

I’m still getting the same results where it display the delete command, but does not actually delete any files. hhmmm…

Aurthur,

this is not included with Windows XP. This particular command is part of a Windows resource kit for Server 2003. If you are looking for a copy you can download it from MS ftp site at:

ftp://ftp.microsoft.com/reskit/y2kfix/x86/forfiles.exe

Great work Scott. A little tweaking and I was able to fit it right into a batch file I am using for file rotation.

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.