﷽
Forfiles is a useful windows command which having similar functionality with Linux find command, it's found to be very handy to perform scripting jobs or any other file administrative works.
forfiles <Parameters to find> /C "Command to be applied to file"
Parameters :
/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.
/?
Displays help at the command prompt.
Variables :
@path – Specified Path
@file – Name of the file(includes extension)
@fname – Name of the file excluding extension
@relpath – Relative path of the file from current folder
@ext – Extension of the file
@fsize – Size of the file
@fdate – Last modified date of the file
@ftime – Last modified time of the file
Usage :
Copy file modified within current day to specified path
ForFiles.exe /p Z:\ /s /d 0 /c "cmd /c copy @file E:\SAPB1Data\BACKUP\LIVE-BACKUP"
Delete file on spescified path which modified later than or equal 3 day
ForFiles.exe /p E:\SAPB1Data\BACKUP\LIVE-BACKUP /s /d +3 /c "cmd /c del @file"
List all .xls files and display location path and last modified date
ForFiles.exe /M *.xls /C "cmd /c echo @path @fdate"