Delete file with the same name with the oldest date

I have a folder that will always have 2 files with the same name, I would like to create a logic to always delete the file with older date, could someone help me?

In order to get the ‘Date Modified’ property of a file, use this:
IO.File.GetLastWriteTime(“C:\trial.pdf”)
Assign that to a variable.
Change the file path.
Then compare.

Thanks and best of luck!
Despi

Another way is read the files in the folder via a ForEach loop

ForEach File in directory.getfiles("YourFolderPath)

Assign FileMetdata = new FileInfo(file)

WriteLine FileMetadata.LastWriteTime.ToString

I was able to get the oldest file but how would I make a condition to delete it?

datecompare.xaml (5.3 KB)
You may want to try this one :slight_smile:

Hello Beatriz,
In this video I do a lot of stuff with Files:

6:55 Delete files
8:15 Delete only PNG files
8:55 Delete all the files except PNG files
9:55 Move files
11:10 Delete files that are older than 2 weeks

Thanks,
Cristian Negulescu

Sign Up Log In