Get file from a directory with specific modified date

I’m trying to find a specific file with a specific modified date in a specific directory. I am currently able to get the latest file in the directory with this snippet: Directory.EnumerateFiles(JsonConfigOut(“NetworkDrive”).ToString(),JsonConfigOut(“ProjectIDInputFile”).ToString,SearchOption.TopDirectoryOnly).OrderByDescending(Function(x) New FileInfo(x).CreationTime).Take(1)

However, is there a way to modify this so I can get a file with a specific modified date?

You can use like File.GetLastWriteTime(path).ToString this will return last modified dateamd time of particular file

Thanks for the reply. Say I want to download the file from 9/28/2018. How would I write it in the code?

Use for each file in dictionary then use
File.GetLastWriteTime(path).ToString and Check in which format you are getting the date time then be use the same format as
File.GetLastWriteTime(path).ToString.contains(yourinput)

Thanks Divyashreem! This seems to be taking be down the right path. Is there a way to add a wildcard to this? The reason I ask is because the filenames are appended with a timestamp (see example below).

\DBPWCWYFS05\PCFDataTechInv\Report 52 - SDLC Cap Report Summary2018-05-17-00-30-47.xlsx

Or is there a way to do it with the filename as opposed to the modified date (whichever is easier):

If filename contains Report 52 - SDLC Cap Report Summary Then
See if filename contains specified date
End If

Simple again add one more condition like previous condition and file.name.tostring.contains(“your constant string”)

If this was solution to your problem plz mark it as solution

You can use modified date or date in filename, but date in filename would require some date extraction and conversions in order to compare with your date you want to look at.

What if you simply just pulled in the files that match a keyword pattern with a wildcard, then you can either compare the WriteTime with a .Where() or using a For each with an If activity inside.

It might look like this:

Assign dateSearch = "10/20/2018"
Assign filePattern = "Report 52 - SDLC Cap Report Summary*.xlsx"
Assign dir = "\\DBPWCWYFS05\PCFDataTechInv"
Assign fileSearchList <Array of String> = Directory.GetFiles(dir, filePattern).Where(Function(f) File.GetLastWriteTime(f).ToString("MM/dd/yyyy") = CDate(dateSeearch).ToString("MM/dd/yyyy") ).ToArray

If fileSearchList.Count > 0
   <perform tasks on fileSearchList.First>

Option of using For each would replace the fileSearchList assignment slightly, like this:

Assign fileSearchList <Array of String> = Directory.GetFiles(dir, filePattern)

For each file in fileSearchList
    If File.GetLastWriteTime(f).ToString("MM/dd/yyyy") = CDate(dateSeearch).ToString("MM/dd/yyyy")
        <perform tasks on file>
        Break

Or something like that…

If these ideas interest you, and you run into problems paste some screenshots of what you are working with and maybe we can identify some solutions for the errors.

Regards.

1 Like

Thanks so much Clayton. Your solution worked flawlessly. Really appreciate your help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.