ForFiles - delete old files, etc.

Tue, 2007-10-16 12:05 by admin · Forum/category:

Table of contents

for this article

Introduction

A few Windows resource kits contained the sometimes very useful tool: forfiles.exe

It can be used to process large numbers of files automatically or in a script to delete old files, among other things.

You can download the version that is included in Windows 2003 Server here. Its file size is 48,640 bytes. If you are not sure which version to use, use this one.

The new version has a strange defect. It throws an error on my computer when it encounters a Google Earth *.kml file. On the other hand it has at least one nicety—it automatically wraps variables like @file in quotes, so you don't have to add escaped quotes (like \") to the command string.

An older, much smaller version is still making the rounds, but it has a different switch format, Unix style, using minus signs, rather than slashes. You can download a lean, good version here or here or a somewhat inflated version directly from Microsoft and these or other versions from a few other places. forfiles.exe version 1.1 is 10,512 bytes long. The inflated version from the Microsoft ftp server is 12,048 bytes long. Be wary of longer files, as they may be virus-infected. All versions, if used properly, are suitable to batch delete old files.

In the following examples, remove "My ", including the space, if you work on Windows Vista, where these special folders have been renamed not to include "My ". Also replace "Documents and Settings" with "Users" on Vista.

Examples for the new version using Windows-style switches (like /c)

Prevent batch processing before at least 5 days have passed

cd /d "%USERPROFILE%\My Documents\TEMP"

if exist SkipDays.sig goto SkipDaysOk

echo off
echo.
echo #################################################################################
echo Error: "%USERPROFILE%\My Documents\TEMP\SkipDays.sig" missing. Press Ctrl + C if you do not want to perform automatic after-logon maintenance. Otherwise:
echo #################################################################################
echo.
pause
echo on

:SkipDaysOk

rem Delete signal file if it is 5 days old:
forfiles /m SkipDays.sig /c "cmd /c del SkipDays.sig" /d -5
if exist SkipDays.sig goto Finish

rem At least 5 days have passed. Create new signal file:
echo Signal file for regular automatic after-logon maintenance>SkipDays.sig

rem Continue processing after 5 days have passed:

rem ...

:Finish

Batch file to delete old files

The following examples show each deleted file name and then wait 8 seconds before actually deleting the file. This allows you to break off with Ctrl + C before the file is actually deleted. Increase the number of seconds if you think that 8 is too little time to decide.

The examples require sleep.exe or wait.exe or a similar tool. If you don't have anything like that, you can use ping to a nonexistent address and use its timeout.

Delete all files in the standard TEMP folders and all their subfolders after 9 days:

forfiles -p"%SYSTEMROOT%\TEMP" -s -c"cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" -d-9
forfiles /p "%TEMP%" /s /c "cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" /d -9

Delete all *.TMP files in the system root (like C:\WINDOWS) that are older than 9 days:

forfiles /p "%SYSTEMROOT%" /m *.TMP /c "cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" /d -9

Delete old IIS log files:

forfiles /p "%SYSTEMROOT%\system32\Logfiles\HttpErr" /c "cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" /d -99
forfiles /p "%SYSTEMROOT%\system32\Logfiles\W3Svc1" /c "cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" /d -99
forfiles /p "%SYSTEMROOT%\system32\Logfiles\SmtpSvc1" /c "cmd /c if @isdir==FALSE echo del @file & sleep 8 & del @file" /d -99

Similar examples for the old version using Unix-style switches (like -c)

These examples do not show the folder check (@ISDIR==FALSE), but the old version has the ability as well, so you can add that, if you like.

Start a batch process only after at least 5 days have passed

cd /d "%USERPROFILE%\My Documents\TEMP"

if exist SkipDays.sig goto SkipDaysOk

echo off
echo.
echo #################################################################################
echo Error: "%USERPROFILE%\My Documents\TEMP\SkipDays.sig" missing. Press Ctrl + C if you do not want to perform automatic after-logon maintenance. Otherwise:
echo #################################################################################
echo.
pause
echo on

:SkipDaysOk

rem Delete signal file if it is 5 days old:
forfiles -mSkipDays.sig -c"cmd /c del SkipDays.sig" -d-5
if exist SkipDays.sig goto Finish

rem At least 5 days have passed. Create new signal file:
echo Signal file for regular automatic after-logon maintenance>SkipDays.sig

rem Continue processing after 5 days have passed:

rem ...

:Finish

Batch file to delete old files

Delete all files in the standard TEMP folders and all their subfolders after 9 days:

forfiles -p"%SYSTEMROOT%\TEMP" -s -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-9
forfiles -p"%TEMP%" -s -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-9

Delete all *.TMP files in the system root (like C:\WINDOWS) that are older than 9 days:

forfiles -p"%SYSTEMROOT%" -m*.TMP -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-9

Delete old IIS log files:

forfiles -p"%SYSTEMROOT%\system32\Logfiles\HttpErr" -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-99
forfiles -p"%SYSTEMROOT%\system32\Logfiles\W3Svc1" -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-99
forfiles -p"%SYSTEMROOT%\system32\Logfiles\SmtpSvc1" -c"cmd /c echo del \"@FILE\" & sleep 8 & del \"@FILE\"" -d-99

Please add other good examples.

run forfiles from a batch file

Wed, 2008-12-17 14:39 by mpniel

May be it is a bug: I run the forfiles command from windows prompt and it work ok.
When i copy paste the same command into a cmd file it goes into an infinite loop.
Does anyone know the solution?

run forfiles from a batch file

Thu, 2009-05-28 18:43 by Mehrjan

Windows accept space in file names. However, don't put space in your batch file name. I mean use one word as your batch file name!

Why not?

Sat, 2009-05-30 16:06 by admin

Spaces in filenames are normal and should work just fine. Depending on circumstances you may have to enclose the filename in quotes when it is used.

%%

Wed, 2008-12-17 17:47 by admin

In some situations the percent signs have to be duplicated when a command is used in a batch file, for example the percent sign that starts the variable in a for loop command.

I don't know the details off-hand. You have to read it up.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.