On one of our servers we have a batch processing program. It is old and clunky, but it gets the job done.
An irritating bug it has is that it leaves temp files around in the c:\windows\temp folder.
One day we noticed the server was getting slow – finally realized that the temp folder had over 100,000 files!
We don’t have the code for this program, so we can’t fix the bug. An easy fix would be to setup a scheduled task to trash that folder every night.
Oscar would like to have all those files every night, but the problem with this solution is it can cause problems with apps that actually are *using* those temp files.
What we needed was a way to delete files that were over 1 day old. This was enough to make sure the temp file was no longer used by any of our processing apps.
After searching around, I found a command line program that Microsoft included with some versions of windows. It is called “forfiles”
ForFiles will process a command against files and folders that match your criteria. It allows you to set wildcards, pick only folders, or only files…and more importantly it allows you to specify something like “I only want files that are more than 1 day old”
First, let me show you the possible command line arguments for it:
/P Path Name - Indicates the path to start searching. The default folder is the current working directory
/M Search Mask - Searches files according to a searchmask. The default searchmask is '*' .
/S Sub Directories Instructs forfiles to recurse into subdirectories. Like "DIR /S".
/C Command Indicates the command to execute for each file. Command strings should be wrapped in double quotes.
The following variables can be used in the command string:
@file - returns the name of the file.
@fname - returns the file name without extension.
@ext - returns only the extension of the file.
@path - returns the full path of the file.
@relpath - returns the relative path of the file.
@isdir - returns "TRUE" if a file type is a directory, and "FALSE" for files.
@fsize - returns the size of the file in bytes.
@fdate - returns the last modified date of the file.
@ftime - returns the last modified time of the file.
To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (ex. 0x09 for tab). Internal CMD.exe commands should be preceded with "cmd /c".
/D date Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "yyyy/MM/dd" format; or selects files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. valid "dd" number of days can be any number in the range of 0 - 32768."+" is taken as default sign if not specified.
Cool. It has everything we need.
Lets start out with the path. We want it to look in c:\windows\temp
forfiles -p "c:\windows\temp"
I want to include subfolders:
forfiles -p "c:\windows\temp" -s
Using the date option, I tell it anything over 1 day old:
forfiles -p "c:\windows\temp" -s -d -1
I only want to delete the files that end with .tmp:
forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp
And finally, the command I want to execute against the files it finds is del:
forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp
-c "cmd /c del /f /q @path"
Note the use of the @path variable – it is one of the many variables forfiles recognizes.
So there it is – a one line command that will delete files from our temp folder over 1 day old:
forfiles -p "c:\windows\temp" -s -d -1 -m *.tmp -c "cmd /c del /f /q @path"
If your copy of windows does not have forfiles, you can download it from the Microsoft FTP Server here:
ftp://ftp.microsoft.com/ResKit/y2kfix/x86/
I am really impressed with this little utility, and the possibilities are endless. I highly suggest keeping a copy on your flash drive.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools
{ 7 comments… read them below or add one }
Hi Steve,
Have a question for you. Any idea what versions of Windows have this pre-installed?
I am asking because I would like to use something like this in a logon script, but not sure if all machines will support it.
Thanks, and keep up the great posts.
I believe these versions of Windows have it by default:
Windows 2003
Windows 2008
Windows Vista
Windows 7
These versions do not:
Windows 2000
Windows XP
Hope that helps.
Great tip Steve! It didnt quite work as given above but with a few searches I got it to work perfectly. Saved me from coding it another way. Have it running every 2 days as a scheduled task and its great!
I have been doing just this for years using the freeware utility TDEL, which you can still get here:
The T-Utils homepage
http://tutils.cjb.net/tdel.htm
This is GREAT!! How about the folders? Can a similar script delete the folders as well if the contents in there are “old”?
Tim – I’m using the following to delete the folders (and files) if the contents are “old”:
forfiles -pc:\windows\temp\ -s -c”CMD /c if @ISDIR==TRUE rd /s /q @FILE”
Hope this helps
I am not able to delete using C:\WINDOWS\system32\forfiles.exe -p %SRCDIR% -d -1 -c “cmd /c del /F /Q @path”
Its working in some systems and not in some others?? No idea Why ??