The Ďfindí Command is a very powerful Linux/Unix command which provides various options for searching files based on different criteria. One of these options allows users to search for files based on the modification/access/creation time of the file. In Windows, we donít have such a powerful command. But we do have a command to search for files based on the file modification time. It canít be used to find files based on file creation/access time. Still this command is much better than nothing at all. Below you can find how to use the 'forefiles' command.
Find files modified in the last 2 months:
forfiles /P directory /S /D +60
This command searches for files created in the folder (specified with /P) in the last 60 days. Specifying /S makes it search for such files recursively in all subfolders.
Get the list of files in the current folder which are modified in last 5 days.
forfiles /S /D +5
Note that we have not used /P as we want to search in the current working directory only.
Get the list of files which are not modified in the last 5 days.
forfiles /S /D -5
If there are no files meeting the condition, the command prints the following message.
H:\>forfiles /S /D +8
ERROR: No files found with the specified search criteria.
Find files based on modification date:
To find files modified after September 1st 2013, we can run the below command.
forfiles /P directory /S /D +09/01/2013
To find files modified before July 21st 2013:
forfiles /P directory /S /D -07/21/2013
Execute commands on the files selected:
forfiles has an equivalent functionality similar to -exec option with linux/unix find command. This can be used to run commands on the files set returned by the command.
The syntax of the command is:
forfiles /D date /C "cmd /c command @file"
Move files to another folder based on modification time.
Letís say we want to move the files which are not modified in the last 5 days to another folder (G:\archiveFolder). The command for this would be as below.
forfiles /S /D -5 /C "cmd /c move @file G:\archiveFolder"
This command looks and processes files in subfolders also, Ď/Sí can be removed to perform this only for the files in the current folder.
Delete files in the current folder which are modified in the last 7 days.
forfiles /D +7 /C "cmd /c del @file"
Be cautious while running these commands, verify that you are deleting the right set of files, otherwise the data lost may not be recoverable. Use these commands at your own risk.
To remove files from subfolders also:
forfiles /S /D +7 /C "cmd /c del @file"
Thanks for visiting mswintips.com.
Return to Index Page
Copyright ©2018 CSN All Rights Reserved