Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


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
  '//delete all *.txt files from c:\temp older than 5 days
  DeleteOldFiles("C:\temp", "*.txt", 5)
End Sub

'//This will delete files with specified pattern which are older than specified days
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)
      '//write full path of this file
      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 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.