Forfiles command
The forfiles command selects one or more files, and executes another command on them. It can select files on criteria including file name and modification time. It can be used in the command line or as part of a batch job.
Availability
The forfiles command was introduced as an optional component of Windows NT. Beginning with Windows Vista, it was included in the standard Windows operating system. It's also available as part of Windows 7, Windows 8, Windows 10, and Windows 11.
Its executable file is installed at %WINDIR%/System32/forfiles.exe.
Syntax
FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -]{MM/dd/yyyy | dd}]
/P pathname | The path to start searching for files. If not specified, the current directory (.) is used. |
/M searchmask | Searches files (and directories, if the /S option is specified) whose name matches a search mask searchmask, which may contain wildcards. The default search mask is "*" (an asterisk), which matches all files and directories. |
/S | Recurse into subdirectories, as with "DIR /S". |
/C command | Execute command on each file where command is a command string, enclosed in double quotes. The default command is "cmd /c echo @file", which displays the name of the file. The following variables can be used in the command string: @file – returns the name of the file, with no path information. @fname – returns the file name without extension. If the file has multiple extensions, e.g., file.txt.doc, only the trailing extension is truncated, e.g., a result of file.txt. @ext – returns only the extension of the file. If the file has more than one extension, only the trailing extension is returned. @path – returns the full path of the file, including the file name. @relpath – returns the relative path of the file. @isdir – returns TRUE if a file type is a directory, and FALSE for files. @fsize – returns the size of the file in bytes. @fdate – returns the last modified date of the file. @ftime – returns the last modified time of the file. To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (e.g., 0x09 for the tab character). All commands should be preceded with "cmd /c," although it can be omitted if the command is an external command which requires no arguments. (For a detailed explanation, see this thread on Stack Overflow.) |
/D [ |
Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "MM/DD/YYYY" format. Can also select files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. A valid "dd " number of days can be any number in the range of 0–32768. "+" is taken as default sign if none is specified. |
Examples
forfiles /d -30
List the name of any file in the current directory not modified in the last 30 days.
forfiles /d -30 /c "cmd /c echo @path @fdate"
Same as the command above, but displays the complete path with file name, and the date of the file's last modification.
forfiles /d +"03/15/2018"
List all files in the current directory modified after March 15, 2018.
forfiles /d -"03/15/2018"
List all files in the current directory not modified after March 15, 2018.
forfiles /m "*.txt" /c "cmd /c notepad @file"
For each file in the current directory with the extension .txt (text files), open the file using Notepad.
forfiles /m "*.jpg" /c "cmd /c mspaint @file"
For each file in the current directory with the extension .jpg (JPEG files), open the file using Microsoft Paint.
forfiles /s /p "C:\Users\myuser\Downloads" /m "*.zip" /d -"03/15/2018" /c "cmd /c move @path C:\oldfiles"
For each .zip (zip files) in C:\Users\myuser\Downloads or its subdirectories, if the file was last modified before March 15, 2018, move it to the directory C:\oldfiles.
forfiles /s /p "C:\Users\myuser\backups" /m "*.zip" /d -90 /c "cmd /c del @path"
For each zip file in the directory C:\Users\myuser\backups or its subdirectories, if the file has not been modified in the last 90 days, delete it.
forfiles /s /p . /m "*.zip" /c "cmd /c move @file @fname"
For each file in the current directory and all subdirectories, remove the file name extension from the file name, if it exists. If the file name has multiple extensions, only the last one is removed. For instance, file.bak would be renamed to file, and file.doc.bak would be renamed to file.doc.