Find Files in Created Between a Date Range

Find files in created between a date range

You can use the below to find what you need.

Find files older than a specific date/time:

find ~/ -mtime $(echo $(date +%s) - $(date +%s -d"Dec 31, 2009 23:59:59") | bc -l | awk '{print $1 / 86400}' | bc -l)

Or you can find files between two dates. First date more recent, last date, older. You can go down to the second, and you don't have to use mtime. You can use whatever you need.

find . -mtime $(date +%s -d"Aug 10, 2013 23:59:59") -mtime $(date +%s -d"Aug 1, 2013 23:59:59")

How to Find Files within A Date Range

You can get the output of a command using For /F

for /f "usebackq" %%a in (`powershell.exe -command {& "(get-date).AddDays(-7).ToString('MM/dd/yyyy')"}`) do set dateRef=%%a
forFiles /p <path here> /d +%dateRef%

Get list of files recursively based on a date range

There actually is a way to do this by using WMI and executing queries on CIM_DataFile. The subroutine below would recursively query each subfolder and gather files based on the CreationDate property.

Sub WMIGetFile()
Dim strComputer As String
Dim strDateFrom As String
Dim strDateTo As String
Dim fso, f, subf, oWMI, colFiles, cf

strComputer = "."
strDateFrom = "20180101000000.000000+00" ' 01/01/2018
strDateTo = "20191231000000.000000+00" ' 12/31/2019

Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder("C:\FolderName\")

For Each subf In f.SubFolders
Debug.Print subf.Path
Set colFiles = oWMI.ExecQuery( _
"SELECT * FROM CIM_DataFile" & _
" WHERE Drive = 'C:' AND Path = '\\" & Replace(Right(subf.Path, Len(subf.Path) - 3), "\", "\\") & "\\'" & _
" AND CreationDate >= '" & strDateFrom & "'" & _
" AND CreationDate <= '" & strDateTo & "'")

For Each cf In colFiles
Debug.Print cf.Name
Next cf

Set colFiles = Nothing

Next subf

End Sub

Path is formatted with \\ instead of \ as a path delimiter starting from the drive specified in Drive, hence the Replace(Right()) method.

Also worth noting that WMI dates are formatted as strings by yyyymmddhhmmss.000000.

EDIT:

My brain missed the part where you need to execute this on the main folder as well. In that case, I'd just define it as a function and pass the parameters like this

Sub WMIGetFile()
Dim fso, f, subf, oWMI
Dim strComputer As String

strComputer = "."

Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.getFolder("C:\FolderName\")

QueryCIMDATAFILE oWMI, f

For Each subf In f.SubFolders
QueryCIMDATAFILE oWMI, subf
Next subf

End Sub

Function QueryCIMDATAFILE(conn, path)
Dim colFiles, cf

Dim strDateFrom As String
Dim strDateTo As String

strDateFrom = "20180101000000.000000+00" ' 01/01/2018
strDateTo = "20191231000000.000000+00" ' 12/31/2019

Set colFiles = conn.ExecQuery( _
"SELECT * FROM CIM_DataFile" & _
" WHERE Drive = 'C:' AND Path = '\\" & Replace(Right(path.path, Len(path.path) - 3), "\", "\\") & "\\'" & _
" AND CreationDate >= '" & strDateFrom & "'" & _
" AND CreationDate <= '" & strDateTo & "'")

For Each cf In colFiles
Debug.Print cf.Name
Next cf

Set colFiles = Nothing
End Function

Find range of files using dates as filename and output as array

Consider piping the 'find' into 'awk' perform the filtering.

If all you files are in the same folder, you can replace the find ... with ls *.csv

For large number of files, calling date on each file can be slow. Using find/awk will usually be much faster.

The awk one-liner split the file into component based on '/' (-F/), than filter on the last component ($NF)

#! /bin/bash
from=2017-07-18
last=2019-07-18
find . -name '*.csv' |
awk -F/ -v FROM="$from" -v LAST="$last" 'match($NF, "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\.csv") && $NF >= FROM && $NF <= LAST''

Find all files containing the filename of specific date range on Terminal/Linux

Complete find + bash solution:

find . -type f -regextype posix-egrep -regex ".*CAPTURE04\.[0-9]{14}\.jpg" -exec bash -c \
'fn=${0##*/}; d=${fn:10:-4};
[[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0"' {} \;

  • fn=${0##*/} - obtaining file basename

  • d=${fn:10:-4} - extracting datetime section from the file's basename

  • [[ $d -ge 20171020083000 && $d -le 20171022093000 ]] && echo "$0" - print the filepath only if its datetime "section" is in specified range

Find files modified between two dates on Linux machine

If you want to do it on the Unix command line, try using find with the -mtime option.

Example:

find /home -iname ".c" -mtime 2 -mtime -4

will choose files modified two to four days ago.

How to get the files within a directory using a date range?

Let's add Select where we turn fileName into an anonymous type instance in which we can store both name and date:

var dateFiles = Directory
.EnumerateFiles(filesPath)
.Select(fileName => new { // given fileName
name = fileName, // store name
date = new FileInfo(fileName).CreationTime.Date // ... and date
})
.Where(file => file.date >= startDate && // filter by date
file.date <= endDate)
.Select(file => file.name); // finally, we want name only

Please, notice EnumerateFiles: we don't want to wait whne all the files be collected but ready to process file after file.

Get files between date range in windows PowerShell

You'll want to use the -and operator instead of -or to express "start < LastWriteTime < end"



Related Topics



Leave a reply



Submit