How to remove older than n-days files in windows

Syntax :

forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c “<Command>”] [/d [{+|-}][{<Date>|<Days>}]]

/p <Path> : Specifies the path from which to start the search. By default, searching starts in the current working directory.

/m <SearchMask> : Searches files according to the specified search mask. The default search mask is *.*.

/s : Instructs the forfiles command to search into subdirectories recursively.

/c “<Command>” : Runs the specified command on each file. Command strings should be enclosed in quotation marks. The default command is “cmd /c echo @file”.

/d [{+|-}][{<Date>|<Days>}] : Selects files with a last modified date within the specified time frame.

Selects files with a last modified date later than or equal to (+) or earlier than or equal to (-) the specified date, where Date is in the format MM/DD/YYYY.

Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.
Valid values for Days include any number in the range 0–32,768. If no sign is specified, + is used by default.

Example:
To remove xml files which are older than 90 days from D:\logs\SH we can execute below command from command prompt

forfiles -p “D:\logs\SH” -s -m *.xml /D -90 /C “cmd /c del @path”

By: Parashuram Jadhav

No Comments

Leave a Comment

Your email address will not be published.