CMD forfiles select files to batch from folders or trees (convenient for batch deletion of Files N days ago)


FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {yyyy-MM-dd | dd}]

Select a file (or group of files) and place it on that file
Execute a command. This helps with batch jobs.

Parameter list:
/P pathname indicates the path to start the search. The default folder is currently working
Contents (.).

/M searchmask searches for files based on the search mask. The default search mask is’ * ‘.

/S instructs forfiles to recurse to subdirectories. Like “dir / s”.

/C command represents the command executed for each file. The command string should
Use double quotes.

The default command is “CMD / C echo @ file”. Following variables
It can be used in the command string:
@File – returns the file name.
@Fname – returns the filename without the extension.
@Ext – returns only the extension of the file.
@Path – returns the full path to the file.
@Relpath – returns the relative path of the file.
@Isdir – returns “true” if the file type is directory;
If it is a file, return “false”.
@Fsize – returns the file size in bytes.
@Fdate – returns the date the file was last modified.
@Ftime – returns the time when the file was last modified.

To include special characters on the command line, use 0xhh
Form uses hexadecimal code (for example, 0x09 is tab).
The internal cmd.exe command should start with “CMD / C”.

/D date select a file whose last modified date is greater than or equal to (+),
Or less than or equal to the date specified by (-) in “yyyy MM DD” format;
Or select a file whose last modified date is greater than or equal to (+)
Current date plus “DD” days, or less than or equal to (-) current
Date minus “DD” days. Valid “DD” days can be
Any number in the range 0 – 32768. If not specified,
“+” is used as the default symbol.

/? display help message.

For example:
FORFILES /S /M *.txt /C “cmd /c type @file | more”
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C “CMD / C echo @ path 0x09 was changed 30 years ago. “
FORFILES /D 2001-01-01
/C “CMD / C echo @ fname was new on January 1, 2001. “
Forfiles / D + 2012-5-1 / C “CMD / C echo @ fname is new today. “
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C “cmd /c echo @fsize”
FORFILES /M *.txt /C “cmd /c if @isdir==FALSE notepad.exe @file”

Here are some examples
The following is the batch processing for deleting the backup files n days ago. Note: under Windows 2003, for example, XP, because there is no forfiles.exe, it is not tested to copy the 2003 files to the system32 directory of XP
Delete the file with the last modified date 10 days ago in the backup directory of Disk C
forfiles /p “c:\backup” /d -10 /c “cmd /c echo deleting @file … && del /f @path”
The following is a batch process for copying files to other locations one day ago
To copy the files with the last modification date greater than or equal to July 1, 2007 under the root directory of Disk C to the root directory of disk D
forfiles /p “c:\” /d “2007-7-1” /c “cmd /c copy @path d:\”
Built in command line file of Windows Server 2003, suitable for systems above XP
forfiles /m *.bkf /d -28 /c “cmd /c del @file /f”
Delete the BKF file with the extension 28 days ago in the current directory (based on the current system time)
Forfiles automatically delete files 7 days ago delete
2008-09-12 15:01
Try forfiles.exe.
forfiles /p “d:\test” /s /m *.* /d -7 /c “cmd /c del @path”
Make batch file. Bat file and forfiles.exe are in the same directory. Create a shortcut to bat file at startup.

For files is not available in some systems, so it’s convenient for you to use