Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to delete files older than x days vb.net

I have a basic bit of code which I am using to delete text files within a given directory:

For Each file As IO.FileInfo In New IO.DirectoryInfo(filePath).GetFiles("*.txt")
    If (Now - file.CreationTime).Days > intdays Then file.Delete()
    Next

filePath is the directory where the files reside.

intdays is a variable which determines how many days the files should be retained for.

To test the code i set intdays to 0, assuming that it would delete any files in the directoy. However it doesn't, but creates no errors.

The time "Now" is #2/8/2012 13:59:00 PM# which is greater than 0. But I'm confused as to why it doesn't then delete the file?

like image 998
A Smith Avatar asked Feb 08 '12 13:02

A Smith


1 Answers

The difference between the dates is less than a day.

You are calling the Days property, which is an integer type - this will be 0 for intervals that are less than a day long.

like image 94
Oded Avatar answered Sep 30 '22 17:09

Oded