Forfiles is a useful windows command to select a set of files and then run a command on each of the files. It’s similar to the functionality of find command on Linux OS.
forfiles <Criteria to select files> /C "command to applied on each of files selected"
The criteria we can use to select files:
(Option /D) Modifies date or number of days it was last modified from
(Option /M) Search files based on names
(Option /S) Look for files in sub-directories also
(Option /P) Look for files in Specific directory
forfiles <Criteria to select files> /C "command to applied on each of files selected"
The criteria we can use to select files:
(Option /D) Modifies date or number of days it was last modified from
(Option /M) Search files based on names
(Option /S) Look for files in sub-directories also
(Option /P) Look for files in Specific directory
Find all excel files modified 10 days back in the current folder and subfolders
forfiles /D -10 /S /M *.xlsx /C "cmd /c echo @path"
In the above command @path is used to print the complete absolute path of the file. Similarly we can use below variables in the command part.
@file : Name of the file includes extension
@fname : Name of the file excludes extension
@relpath : Relative path of the file from the current folder
@ext : Extention of the file
@fsize : Size of the file
@fdate : Last modified date of the file
@ftime : Last modified time of the file
delete all log files created in the last 1 month
forfiles /D +30 /S /M *.log /C "cmd /c del @file"
Copy/Backup files modified after 1st Jauary 2015
forfiles /D 01/01/2015 /M * /C "cmd /c copy @file D:\backupfolder\"
Get list of all picture files with their size
forfiles /S /M *.jpeg /C "cmd /c echo @path @fsize"
Get list of all exe’s and their last modified date
forfiles /M *.exe /C "cmd /c echo @path @fdate"
Get list of files which is less than given size
forfiles /S /M *.txt /C "cmd /c if @fsize LSS 1000 echo @file"
List all sub-directories in a given folder
forfiles /M * /C "cmd /c if @isdir==TRUE echo @file"Recursively rename file extentions
If you want to rename files from one extension to another, recursively in all sub folders, then you can use the below command.forfiles /S /M *.ext1 /C "cmd /c rename @file @fname.ext2"Recursively remove file extentions
forfiles /S /M *.ext /C "cmd /c rename @file @fname"Recursively add prefix in filenames
forfiles /S /M *.jpg /C "cmd /c rename@file photo@file"
No comments:
Post a Comment