Public Function delOldFiles(delDays as integer, scanDir as string)
Dim fs As Object
On Error GoTo errorHandler
Set fs = CreateObject("Scripting.FileSystemObject")
' Kontrollera om katalogen existerar
If Dir$(scanDir, vbDirectory) <> "" Then
' DIRECTORY DOES EXIST, SO GO ON ...
Dim fileDate As Date ' Deklarera variabler.
Dim f, f1, fc
Set f = fs.GetFolder(scanDir)
Set fc = f.Files
For Each f1 In fc
fileDate = f1.DateCreated
If DateDiff("d", fileDate, Now) > delDays Then
f1.Delete
End If
Next
End If
Set fs = Nothing
Exit Function
errorHandler:
Resume Next
End Function