≡ Menu

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 7 days

First we need to get the the date for current day – 7 days and use it in the below command.

forfiles /P directory /S /D +(today'date - 30 days)

For example, if today’s date is Jan 11th 2015, if we need to get the files modified in the last 7 days, you can use the below command.

forfiles /P directory /S /D +01/04/2015

This command search for files created in the folder(specified with /P) in the last 7 days. Specifying /S makes it search for such files recursively in all subfolders.

Please note that /D +(number of days) is practically not useful. This option says to search for the files modified after n days from today’s date.

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 3 days ago 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 were updated one month ago

forfiles /D -30 "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 -30 "cmd /c del @file"
{ 12 comments… add one }
  • Tarun November 25, 2013, 11:45 am

    How to find files that are created on the same day?

    • admin January 1, 2014, 2:45 pm

      for /F "tokens=2" %i in ('date /t') do dir /T:C | findstr /C:%i /B
      This command gets the list of all the files from the current folder that are created on the same day. You do not need to specify the date, the command automatically picks the current date.

      You can add /s option to dir command to search in sub directories also. However, the file names will not have full path names, they are printed folder by folder. So you can make out which folder a file belongs to.

  • Sean May 1, 2014, 6:10 pm

    I can not get the forfiles to work for dates modified within # of days (your example: forfiles /P directory /S /D +30)

    Here is what i have:
    forfiles /P C:\DfsRoots\CopyFolders\testdelete /s /m *.* /d +30 /c “cmd /c XCOPY @file c:\DfsRoots\CopyFolders\move”

    It works when i put “-30” for equal to and older than 30 days but not what i have above. any ideas? My only thought was that “+30” is set to a future modified date, which i would assume is rare. Even when i remove my XCOPY cmd it errors out and says there are no files specified.

    • admin January 11, 2015, 11:28 pm

      Hello Sean, you were correct. /D +n option is not making any sense.
      I have updated the post reflecting this.

  • Andy December 3, 2014, 4:23 pm

    Is it possible to move the files with the directory structure intact?

  • Sparsh March 23, 2015, 5:58 am

    How to find last modified file in a directory

  • Chieh April 7, 2015, 2:43 am

    about step 4

    4: Move files to another folder based on modification time

    => folder path should be xxxxxxxxxx D:\archiveDir”

  • Gaurav May 5, 2015, 5:40 am

    Hi ,

    Is there a way i can compare file modified time with system time and perform any operation based on it . Lets say, if file modified time is less then system date by 20 minutes, file should be copied to some location.

    Your response will be highly appreciated.

  • sean June 12, 2015, 5:18 pm

    this is a typo:
    forfiles /S /D -3 /C “cmd /c move @file D;\archiveDir”

    should be:
    forfiles /S /D -3 /C “cmd /c move @file D:\archiveDir”

    otherwise users will get invalid syntax errors.

  • Dave July 1, 2015, 9:01 am

    If you want to deal with file creation/access time, you can do it in Powershell with something like:

    $limit = (Get-Date).AddDays(-30)
    Get-ChildItem -Path $path -Recurse | Where-Object { !$_.PSIsContainer -and $_.lastAccessTime -lt $limit } | Remove-Item -Force

  • KoLuNar September 2, 2015, 1:27 pm

    Hi, I was able to load files according to Modified date using a command below,
    ‘z: & forfiles /m *.jpg /s /d 07/16/2015 /c “cmd /c echo @fdate @ftime @path”‘

    But I wanna know how to load files according to Created date. Any help would be very appreciated.

    A question’s been asked on stackoverflow but no answer yet. Please take a look at the link for the details http://stackoverflow.com/q/32293517/3327081

  • Raghu May 19, 2016, 11:49 am

    I have folder it contained 30 date data and i will delete 15 dates data via command prompt.

    Please suggest the commands

Leave a Comment