|
|
|
Some time I need to write a clenup routine which checks fol old files and delete if older than a specified days. Here is a code snippet which does the same thing. |
Click here to copy the following block | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DeleteOldFiles("C:\temp", "*.txt", 5) End Sub
Public Function DeleteOldFiles(ByVal sPath As String, Optional ByVal sPattern As String = "*.*", Optional ByVal OlderThanDays As Integer = 1) As Integer
Dim myfolder As System.IO.Directory Dim myfile As String Dim fi As System.IO.FileInfo Dim files() As String = myfolder.GetFiles(sPath, sPattern) Try
For Each myfile In files fi = New System.IO.FileInfo(myfile) Debug.Write(fi.FullName) If fi.CreationTime < Today.AddDays(0 - OlderThanDays) Then fi.Delete() End If Next Catch ex As Exception Throw (New Exception(ex.Message)) End Try End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|