How Can We Help?

Delete Files Older Than XX Days

You are here:
< Back
Delete Files Older Than XX Days
Last Modified: 01 Mar 2004
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*** PLEASE NOTE: Link(s), If Provided, May Be Wrapped ***


The following batch file will step through the specified
starting folder (%@FOLDER%) and its subfolders, and delete
all files older than %@AGE%.

It requires the use of Windows NT4 or higher, and several
utilities from the Resource Kit, such as FORFILES and
ROBOCOPY v1.95 or higher.

The folder must be the shortname if entered at the command
line. Longname folders should be entered directly into the
batch file.


The syntax is:

	DELETEOLD.BAT folder age

Example:

	DELETEOLD D:\LOGS 25


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NOTE: The version below ("C") will only work in Win2K.
      See version "D" for a version compatible with
      both NT4 and Win2K.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@ECHO OFF
 rem ==============================================================
 rem === Initialize Environment Variables
 rem === Last Modified On 06 Jul 2000
 rem ==============================================================
:Variables
 SETLOCAL
 SET @FOLDER=%1& IF /I "%1"=="" SET @FOLDER=C:\TEMP
 SET @AGE=%2& IF /I "%2"=="" SET @AGE=30
 SET @LOGFILE="%@FOLDER%\DeleteOld.LOG"


 rem ==============================================================
 rem === Recurse Through Subfolders And Delete Old Files
 rem === Last Modified On 17 Oct 2000
 rem ==============================================================
:SetFolders
 NOW Hunting Through "%@FOLDER%" And Subfolders For Old Files... >%@LOGFILE%
 ECHO.                                                          >>%@LOGFILE%
 ECHO.
 ECHO Hunting Through "%@FOLDER%" And Subfolders For Old Files...
 ECHO.
 FOR /F "TOKENS=*" %%D IN ('FORFILES -p"%@FOLDER%" -d-%@AGE% -s -c"CMD /C ECHO @PATH\@FILE"') DO CALL :RemoveFiles %%D


 rem ==============================================================
 rem === Reset Environment Variables and Exit Batch File
 rem === Last Modified On 27 Jun 2000
 rem ==============================================================
:ExitBatch
 ENDLOCAL
 GOTO :EOF


 rem ==============================================================
 rem === SUBROUTINE: Remove Files That Are Over XX Days Old
 rem === Last Modified On 17 Oct 2000
 rem ==============================================================
:RemoveFiles
 rem %* = Current Filename
 ECHO.                   >>%@LOGFILE%
 ECHO -- Purging "%*"...
 ECHO -- Purging "%*"... >>%@LOGFILE%
 ATTRIB -R -S -H "%*"
 DEL "%*"                >>%@LOGFILE%
 GOTO :EOF



Some additional DELOLD tools are listed below. You can
use these versions if you don't have access to a Resource
Kit or you want additional flexibility for your scripts.

CONSOLE UTILS

• DelOld ................. http://www.jsiinc.com/SUBA/TIP0200/rh0274.htm
• DelOld ................. http://www.xs4all.nl/~chi/utilities/delold.html
• zDel ................... http://members.aol.com/pjsoftware/page6.html  (Dead link)


GUI UTILS

• Delenda ................ http://www.pcworlddownload.com/system-utilities/disk-cleaners/Delenda.htm
• DeleteX ................ http://www.csn.ul.ie/~madman/software/deletex.htm
• DelOld ................. http://www.michna.com/software.htm#DelOld
• DelOld ................. http://www.savilltech.com/delold/
• FDFiles ................ http://www.freewareweb.com/cgi-bin/archive.cgi?ID=193


PERSONAL NOTES

• The various versions below use different utilities and
  techniques to perform the same function as the script
  sample above, but the main difference is that they
  provide command-line support for long file names.


RELATED SCRIPTS (ALSO IN THIS ARCHIVE)

• http://KB.UltraTech-llc.com/Scripts/?File=DelOld-a.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-b.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-c.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-d.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-e.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelOld-f.BAThttp://KB.UltraTech-llc.com/Scripts/?File=DelEmpty.BAThttp://KB.UltraTech-llc.com/Scripts/?File=ZipFiles-A.BAThttp://KB.UltraTech-llc.com/Scripts/?File=ZipFiles-B.BAT


RELATED TOPICS (ALSO IN THIS ARCHIVE)

• http://KB.UltraTech-llc.com/?File=ResKit.TXThttp://KB.UltraTech-llc.com/?File=Scripting.TXThttp://KB.UltraTech-llc.com/?File=ToolKit.TXThttp://KB.UltraTech-llc.com/?File=Utils.TXT