18

Is there any way to search for files in a directory based on date? I want to find all files with created date greater than a specific date, is it possible to do it with dir command?

Vik David
  • 3,640
  • 4
  • 21
  • 29
Karim Harazin
  • 1,463
  • 2
  • 16
  • 34

5 Answers5

41

Just discovered the forfiles command.

forfiles /s /m *.log /d -7 /c "cmd /c echo @path"

Will list all the log files modified more than seven days old, in all subdirectories, though it does not appear to look at the create date. It does support specifying a specific date.

See forfiles /? for more info.

David
  • 19,389
  • 12
  • 63
  • 87
  • Super-useful! (when no PowerShell available) NOTE: If you want files modified after a date you need `+` instead of `-` for the `/d` parameter, so something like `+02/05/2012` instead of `-7`. Also to show the date and time output for each file you could use `cmd /c echo @fdate @ftime @path` – bob Jul 10 '13 at 14:58
  • 1
    Caveat: Woe betide ye who want to use UNC paths with this. Or in many other places in Windows... – underscore_d Jun 21 '16 at 16:06
  • `FORFILES /D` only works with modified dates. To solve UNC issues just use `MKLINK /D`. – Marc Aug 12 '16 at 09:47
  • 1
    For anyone expecting `/d +7` to show you files modified fewer than seven days ago, nope. It will shows you [files modified seven days from now](https://www.windows-commandline.com/find-files-based-on-modified-time/). You have to use a date, like `+02/05/2012`. – Noumenon Nov 07 '17 at 14:49
9

an easier way for me is something like

dir /s *.xlsx | find "07/14/2016"
skelly
  • 91
  • 1
  • 2
6

dir by itself can not filter by date, but you can parse the output of dir using for command. If in your country dir prints the date in YMD format, then you only need to compare it with given date. If the order of date parts is different, then you have to use another for command to parse the date and change it to YMD. This will display a list of files modified after 5th Februrary.

@Echo Off

for /f "usebackq tokens=1,4 skip=5" %%A in (`dir /-c`) do (
  if %%A GTR 2012-02-05 echo %%A %%B
)

if does standard string comparison, so at the end you can get additional line if summary line passes the comparison. To avoid it, you can use if %%A GTR 2012-02-05 if exist %%B echo %%A %%B

EDIT: There is even better approach which avoids parsing of dir output and also allows searching by time, not only by date:

@Echo Off

for /r %%A in (*) do (
  if "%%~tA" GTR "2012-02-05 00:00" echo %%~tA %%A
)
MBu
  • 2,880
  • 2
  • 19
  • 25
  • thanks you that's what i need, can i use the hour:minutes with comparison. Finally is there helpful books to learn shell scripting. – Karim Harazin Feb 12 '12 at 15:30
  • To use minutes in comparison, do the following changes: `tokens=1,2,4` and then `if "%%A %%B" GTR "2012-02-05 11:23" echo %%A %%B %%C` To learn, you can start with http://en.wikipedia.org/wiki/Batch_file and then continue with three first external links. – MBu Feb 12 '12 at 21:00
3

Well you cant as far as i know, but this sort of think will work, but still really useless unless you have a short date range ;)

for /R %a in (01 02 03 04 05) do dir | find "02/%a/2012"
Justin
  • 3,255
  • 3
  • 22
  • 20
1

This is easy to do with PowerShell. I know that your question was about cmd, but PS is included in windows 7 and later. It can also be installed on XP and Vista.

Use the Get-ChildItem command (aliased as dir) to get all files. Pipe the output to the Where-Object command (aliased as ?) to return files where the date is greater then (-gt) a specific date.

For Powershell 2.0 (default on Windows 7), you must use a scriptblock:

dir -file | ? {$_.LastWriteTimeUtc -gt ([datetime]"2013-05-01")}

For Powershell 3.0 (default on Windows 8) and up you can use this simpler syntax instead:

dir -file | ? LastWriteTimeUtc -gt ([datetime]"2013-05-01")

The dir -file command returns a collection of System.IO.FileInfo objects. This file object has many properties that you can use for complex filtering (file size, creation date, etc.). See MSDN for documentation.

bouvierr
  • 3,563
  • 3
  • 27
  • 32
  • I tried: dir . *.svg | ? LastWriteTimeUtc -gt ([datetime]"2013-05-01") but received "Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "LastWriteTimeUtc" value of type "System.String " to type "System.Management.Automation.ScriptBlock". – Chris Nevill Oct 01 '14 at 11:23
  • The syntax I used works for PS 3.0 and up I believe. If you have PS 1.0 or 2.0, you must use a scriptblock like so: `| ? {$_.LastWriteTimeUtc -gt ([datetime]"2013-05-01")}` – bouvierr Oct 01 '14 at 11:37
  • Ah I'm on Windows 7 with PS 2. Thanks – Chris Nevill Oct 01 '14 at 16:56
  • dir -file throws "A parameter cannot be found" on 2.0 – srage Feb 13 '15 at 04:57
  • Can someone please explain why this command finds all files modified on the date but disregards the time? dir -s -file | ? LastWriteTimeUtc -gt ([datetime]"2015-03-07 16:00:00") That is, it finds all file modified after 2015-03-07 00:00:00, which is NOT what I want. I want all files modified after 4pm. Thanks in advance. – Joel Finkel Mar 08 '15 at 21:52
  • @JoelFinkel it works fine for me. BTW you're filtering using UTC time so that might explain the difference. – bouvierr Mar 09 '15 at 22:02
  • i believe for local time, you want `LastWriteTime`. Seems to work for me. – johny why Sep 02 '21 at 16:31