Monday, July 28, 2014

Find files based on modified time

‘find’ is a very powerful Linux 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 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 something is better than nothing. :| Below you can find how to use this command.

1. Find files modified in the last 1 month

forfiles /P directory /S /D +30
This command search for files created in the folder(specified with /P) in the last 30 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 3 days.
forfiles /S /D +3
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 3 days
forfiles /S /D -3
If there are no files meeting the condition, the command prints the following message.
D:\>forfiles /S /D +3
ERROR: No files found with the specified search criteria.

2. Find files that were last modified 1 month back

forfiles /P directory /S /D -30

3. Find files based on modification date

To find files modified after 1st August 2013, we can run the below command
forfiles /P directory /S /D +08/01/2013
To find files modified before 20th August 2013:
forfiles /P directory /S /D -08/20/2013

Execute commands on the files selected

forfiles has an equivalent functionality similar to -exec option with linux 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"

4: 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 3 days to another folder(D:\archiveDir). The command for this would be as below
forfiles /S /D -3 /C "cmd /c move @file D;\archiveDir"
This command looks processes files in subfolders also, ‘/S’ can be removed to perform this only for the files in the current folder.

5: Delete files in the current folder which are modified in the last 7 days

forfiles /D +7 "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 "cmd /c del @file"

http://www.windows-commandline.com/find-files-based-on-modified-time/

No comments: