My site is ad supported.

Delete By Date Pattern (Command Line Utility)

Version 2.0.0 - Download


Description

Delete/Select files based on a date which utilizes MM and/or DD for file naming patterns, such as log files.

For example, if an application or service logs information to a file which is named according to a date format such as Log_YYMMDD, Log_YYYYMM, Log_DDMMYY, etc. this script can be used to locate them for processing and optionally delete them.

Note: Requires .NET 2.0.

Examples

The following command deletes files following the pattern "Backup_MM-DD-YYYY.zip" from the C:\Backup directory which are older than 3 weeks:

DeleteByDatePattern /D 22 Backup_*- -*.zip /R /P C:\Backup\

The following command deletes IIS log files from all sites which are older than the previous month:

Server 2008 / IIS 7:

DeleteByDatePattern /M 2 u_ex?? ??.log /R /P "C:\inetpub\logs\LogFiles\" /S

Server 2003 / IIS 6:

DeleteByDatePattern /M 2 ex?? ??.log /R /P "C:\WINDOWS\system32\LogFiles\" /S

The following script locates all files matching the pattern "MyApp_YYYYMMDD.log" in the current directory and copies them to a network path before deleting them:

FOR /F "usebackq tokens=*" %%A IN (`DeleteByDatePattern /M 3 MyApp_???? *.log /L`) DO (
  ECHO Processing: %%A
  XCOPY "%%A" "\\NetworkPath\ArchivedLogs\%%A"
  DEL "%%A"
)


My site is ad supported.

Help Information (/?)

DeleteByDatePattern {/M | /D} ToKeep PatternPrefix PatternSuffix
                    {/A | /L | /R [/Q]} [/P Dir] [/S] [/T] [/H]

 /M      Specifies the pattern being used is based on months.
         The maximum allowable value is 12.
 /D      Specifies the pattern being used is based on days.
         The maximum allowable value is 31.
 ToKeep  The number of months or days to keep, including the current.
 PatternPrefix
         The file search pattern placed before the month/day when building
         the search string. If there is no prefix, enter "N/A".
 PatternSuffix
         The file search pattern placed after the month/day when building
         the search string. If there is no suffix, enter "N/A".
 /A      Displays the resulting patterns (with the month/day value inserted)
         which will be used for finding matching files. When this option is
         specified, the files are not searched for, only this information is
         displayed. Enabling the /A switch will disable /T.
 /L      Lists all files matching the pattern, but does not delete them.
 /R      Deletes all files matching the pattern.
 /Q      Quiet mode. Does not display the file names of the processed files.
 /P Dir  The location to search. If this is omitted, the current directory
         will be used.
 /S      Specifies that subdirectories should be searched as well.
 /T      Displays the total number of applicable files as well as the total
         size (in bytes) of the respective files.
 /H      Hides the configuration summary display.


Exit Codes:
    0 = Command completed successfully.
    1 = Invalid parameters.
    2 = Error during processing.

Version History

Version 2.0.0

  • Major rewrite as a .NET command line application. This overcomes limitations caused by batch scripts.

Version 1.1

  • Fixed an issue where date components starting with a 0 (e.g. 01-09) would not be detected as numbers. This caused the script to crash before processing.