I want to know how I can delete files older than x years
Note: my date ismydate(yyyy/mm/dd) i dont know if this can cause any problems or not
This is my code so far:
Dim fso As Variant
Dim directory As Variant
Dim modified As Variant
Dim files As Variant
Private Sub Command1_Click()
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set directory = fso.GetFolder(App.Path & "\log")
Set files = directory.files
For Each modified In files
If DateDiff("Y", modified.DateLastModified, Now) > mydate Then
modified.Delete
Next
End Sub
I am using DateDiff for the first time in my life so please be kind on explaining where I’m mistaken.
DateDiff returns the difference between two dates. You are using it to get the difference in years between the current date and when the file was last modified which is correct but you are then comparing this to a date variable (I think – you haven’t included the definition of myDate). You need something more like:
Where x is an integer number (2 for files older than two years for example).