Friday, August 14, 2009

Delete files older than certain period

Public Shared Sub ClearTemporaryFiles(ByVal folderPath As String, ByVal maxLifeTime As Integer)

Dim timeLimit As DateTime = DateTime.Now.AddMinutes(0 - maxLifeTime)

Dim filePaths() As String = System.IO.Directory.GetFiles(folderPath, "*.jpg")

For i As Integer = 0 To filePaths.Length - 1

Dim filePath As String = filePaths(i)

If (System.IO.File.GetLastWriteTime(filePath) < timeLimit) Then

Try

System.IO.File.Delete(filePath)

Catch

End Try

End If

Next

End Sub

blog comments powered by Disqus