How can I get only files added today from a directory

Hello everyone!
I’m Brazilian, so I used Google Translate to be able to transcribe the message to everyone.

This is being my first post on the forum as I haven’t found any solution to my problem. =/

I’m developing an automation where I need to get from a directory only the files that were added today, either by creation or modification date (files are created and sent to the folder on the same day, so the creation and modification date will always be the same).

Currently, the closest I’ve come is:
Directory.GetFiles(“C:\Temp”).Where(Function (f)Directory.GetCreationTime (f).ToShortDateString = System.DateTime.Now.ToString("dd/MM/yyyy ")).ToArray

However, when I run the automation, nothing happens.

Can anybody help me?

One reason might be, that .ToShortDateString does not have the format “dd/MM/yyyy”.

Because for me it doesn’t, for me it’s “MM/dd/yyyy”.

You can easily check it by getting all files with Directory.GetFiles(“C:\Temp”)

and then looping through them, printing the dates like so:

Directory.GetCreationTime(file).ToShortDateString +" vs " + System.DateTime.Now.ToString("dd/MM/yyyy ")

This is my result:

Other than that, I wouldn’t recommend doing this by comparing strings but rather by comparing the actual dates so it doesn’t matter which format a date is in.

Directory.GetCreationTime(file).Date = System.DateTime.Now.Date

If you use this in your where statement, it should work.

If this has helped you, please mark this answer as the solution.

1 Like

@guintherlanger - Please try this…

Directory.GetFiles("YourFolderName").Where(Function(x) New FileInfo(x).LastWriteTime.Date = Now.Date).ToArray
1 Like

Good afternoon people!
Wow, thanks so much for the agility!

So… I tried the @T0Bi option, and with a few tweaks it worked correctly:

However, the directory contains more than 3,500 files, making the “for each” activity take a while to find my “if”.

I also tried @prasath17’s solution, but it didn’t work. Because I’m in Brazil, the DateTime format that windows uses is “dd/MM/yyyy”, so I couldn’t handle this in “Where”.

Do you have any idea how I could make this “for each” search more efficient or faster? Or some other alternative?

If not, I’ll need to use the first option even if it takes a while, it’s life haha

I didn’t used any datetime formats in my code. So I guess it should work irrespective your country.

Can you please print these statements in your write Line(After put in your pathname) and show your outputs here??

Then your file Modified date should also be in dd/MM/yyyy format right??

1 Like

Hello @prasath17!
Friend, a thousand pardons, I believe that I have made a mistake when typing the code in my project.

I tried again the code you sent and it worked perfectly, really, the DateTime didn’t influence…

My problem is solved, thank you very much.

I intend to participate much more in the forum and be able to help our colleagues like you helped me. You guys are awesome!

1 Like

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