Search notes:

forfiles.exe

Execute a command on selected files.

Command line options

/P pathname
/M Search mask (aka wildcard), default is *
/S Recurse into subdirectories
/D Specify a modification date (format yyyy-mm-dd) that a file needs to be newer (+) or older (-) to be selected
/C specifies command to be executed on matched files (as per /P, /M and /S), see Variables that can be used in commands below.

Variables that can be used in commands

@file name of the file.
@fname file name without extension.
@ext file's extension
@path full path of the file.
@relpath the relative path of the file.
@isdir TRUE if a file type is a directory, and FALSE for files.
@fsize size of the file in bytes.
@fdate last modified date of the file.
@ftime last modified time of the file.

Delete files that are older than 5 days

The following command recursively (/s) deletes files below p:\ath\to\directory that are older than 5 days.
The if @isdir == FALSE makes sure that del is only applied to files but not to directories.
The variable @path is expanded to the absolute path of the matching files. (@file would expand to the file name only).
C:\> forfiles /s /p p:\ath\to\directory /d -5 /c "cmd /c if @isdir == FALSE del @path"

Print files with a modification that is younger than a given date

The following command prints files whose modification date is after 12/01/2019. @fdate and @ftime expand to the printed file's modification date and time.
P:\ath\to\directory> forfiles /s /m *.txt /d +12/01/2019 /c "cmd /c if @isdir == FALSE echo @path [@fdate @ftime]"
In order to reverse the criteria, /d -12/01/2019 should be used.

See also

PowerShell: Find files that are newer than n days
walk.cmd is a batch file that walks trhough a directory tree and executes the passed command. walk.cmd is a script that belongs to the Debugging Tools for Windows.

Index