≡ 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"
36 comments… add one
  • Tarun

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

    • admin

      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

    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

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

  • Andy

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

    • Zak Snell

      Has anyone had a solution for this question? I have the exact same question but have no idea how old this post is or if it is still alive.

      Any help is much appreciated!

  • Sparsh

    How to find last modified file in a directory

  • Chieh

    about step 4

    4: Move files to another folder based on modification time

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

  • Gaurav

    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

    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

    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

    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.

  • Raghu

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

    Please suggest the commands

  • Andrew

    I need to delete the file based on another file. for example. get the date of one file and delete all files in the directory except the main file before that date time. For example.

    three files exist
    file.id 11-Jan-16 11:30AM — Main file where the time needs to be extracted and file to be not deleted
    file.txt 11-Jan-16 11:30 AM — Keep as it matches with file.id date/time
    file.abc 11-Jan-16 11:30 AM– Keep as it matches with file.id date/time
    file.txt 10-Jan-16 11:00 PM — Delete as the time does not match with date/time
    file.abc 08-Jan-16 11:45 PM — Delete as the time does not match with date/time

    can someone please help?

  • Powershell

    Get-ChildItem -Path . -Recurse| ? {$_.LastWriteTime -gt (Get-Date).AddDays(-4)} > f:\test.txt

  • pallavi

    how to find modified files from a particular time. example from 3:00pm of today to now.

  • Adl

    Thanks!

  • Abhishek Chauhan

    Hi guys,

    Can you please help me with below query.

    How to search the zip file in a folder with modification date and move it to another folder ?

    Thanks

  • Bhagyashree

    Hi Need a batch script which will copy the path of the latest file in the specified folder and copy the path in a text file(the path and the file name of the text file will be constatnt).
    Can someone please help me as soon as possible.. Thank you!

  • Subhajit Dutta

    Hi,

    I have some database .bak files. Every day the backup happens and sometimes it fails also.
    I need to delete the old backup files leaving the latest one.
    Can you please help me with a batch script?

  • RUPESH RAMESHCHANDRA SHELAR

    I wanted to move based on the less than max system modified date available

    Name date Modified
    file1 14/05/2018 11:21 AM
    file2 14/05/2018 11:41 AM
    In this case we want to move file1 so apart from max system modified date. All remaining files have to move

    I have tried below command:

    move /-y “C:\app\folder1*.txt” “C:\app\folder2\”
    pause

    other step tried

    Get-ChildItem -Path “C:\app\folder1\”
    Sort-Object LastWriteTime -Descending
    Move-Destination t-Object -Skip 1
    Move-Item -Destination “C:\app\folder2\” |
    i tried again getting error….| C:\app\folder1>Move-Item -Destination “C:\app\folder2\”

    ‘Move-Item’ is not recognized as an internal or external command, operable program or batch file.
    thanks

  • Roy

    How can i modify below code to search .pdf files that have been modified today ?

    for /r %%i in ( *.pdf ) do (

  • Alex

    Suppose I have a list of text of xml files in a directory, how do I find out the oldest file among them? And I want to get the timestamp of that oldest file.
    How do I use forfile and do – while loop together?
    Please help me with an example code.
    Thank you.

  • Flemming Steffensen

    Thank you for the introduction to this command. After reading your page, I created these to fit my exact needs:

    List folders only
    forfiles /C “cmd /c if [@isdir] equ [TRUE] echo @file”

    List files only
    forfiles /C “cmd /c if [@isdir] neq [TRUE] echo @file”

    List folders modified after a date
    forfiles /D +2018-11-19 /C “cmd /c if [@isdir] equ [TRUE] echo @file”

    List folders modified before a date
    forfiles /D -2018-11-19 /C “cmd /c if [@isdir] equ [TRUE] echo @file”

  • ABDOUL

    Hello,

    How can I move the most recent modified file from directory to another one ?

    Thank you !

  • lijo

    How can we delete .BAK files from a NAS share and also remove empty folders and subfolders from the same location of 7 days old files and folders.

    Please any one help to to resolve it in a same command line.

    PushD “\\sharename” &&(“forfiles.exe” /s /d -7 /c “cmd /c del @path”) & PopD

  • Mahesh

    How can I copy a files and folders which are updated or created in last 30 minutes …
    I used the below code that only copies Files and sub-folders but not the content in the sub-folders
    @Echo Off
    Powershell -NoL -NoP -C “&{$ts=New-TimeSpan -M 30;”^
    “GCI “D:\test” -Fi ‘*.*’|?{“^
    “$_.LastWriteTime -gt ((Get-Date)-$ts)}|”^
    %%{CpI $_.FullName ‘D:\test\newfoder’}}”

  • ofir

    Hi
    i need to copy from remote pc, the latest zip file that have been created.
    i’m using
    FOR /F %%I IN (‘DIR “\\ipaddress\c$\temp\*.zip” /B /O:-D’) DO COPY %%I .\localfolder

    in fact – the file Meet the criteria but
    but the script says NOT

    i need to copy the latest *.zip file .

    thanks!

  • casper

    Hi, I have a folder that will create a file every 15min and I just want to keep the latest two copy every time, means I need to deleted the oldest file that I created when a new copy of a file is created. can someone show me how to do so? Thanks.

  • Adam

    I have a corpus of still photos taken over multiple months by my webcam in order to make a time lapse video. I would like to be able to search across all days for photos from the same time , for example, find all photos (in this date range) modified at 12:15 PM. Can someone help me with how to do this with a query via windows file explorer, or recommed another tool? Thanks!

  • Lawrence D Kidd

    Hi,
    I need to sort folders into the subfolders of a directory by the month that they were created.
    I have folders created daily, titled with a two digit number, like “13” for the 13th. When the command is ran tomorrow, I need the folder to be moved to a folder titled “05” for May. This is easy when the batch file is ran the same day, but since it is running the next work day, I am having trouble at the end of the month.

  • Goof

    There is actually a simple solution for your remark:
    “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.”
    You can actually find files modified in the last X days:
    Syntax:
    cd /D && forfiles /M /C “cmd /c forfiles /D – /M @file >nul 2>nul || echo execute your command on @path”
    Example:
    cd /D C:\temp && forfiles /M *.txt /C “cmd /c forfiles /D -3 /M @file >nul 2>nul || copy @path c:\temp2”

    • Goof

      oops, hopefully this syntax is visible:
      cd /D [pathname] && forfiles /M [mask] /C “cmd /c forfiles /D -[number of days] /M @file >nul 2>nul || echo execute your command on @path”

  • Goof

    And for a faster solution:
    Syntax:
    cd /D [path] && for /f %f in (‘dir [searchmask] /B /O-D’) do forfiles /D -[days] /M %f >nul 2>nul && exit || echo execute your command on %f
    Example:
    cd /D C:\temp && for /f %f in (‘dir *.txt /B /O-D’) do forfiles /D -3 /M %f >nul 2>nul && exit || copy %f c:\temp2
    In this syntax, the sorting parameter /O-D makes sure the newest files are checked first. If a file older than /D -[days] is found (no error), than the execution stops (exit). As long as forfiles creates errors (because %f is actually more recent), your command is executed on the (more recent) files.
    Remark: Double the % (=%%f) in case of a batch command file.

  • Charles Volkstorf

    You’re not saying anything about how to get to this command. This is good for people who already know and probably already know what you are describing. It is no good for people who don’t know and could benefit if you didn’t assume everyone is a geek.

  • Michael Rose

    My “FORFILES /?” tells me the only accepted date format is “dd.MM.yyyy”.

Leave a Comment