Delete Old Files (dotNet)

Umm, I needed some way to clean up the files on my Win2K* servers. So here’s a quickie in VB.NET, hope it helps.

' Written by Abdallah Deeb on 02-Mar-2007
' Use at your own risk, I am not liable for any damages or lost files if you use this code.
' 
Imports System
Imports System.IO
Module DeleteOld

    Sub Main()
        Dim sr As StreamReader
        Dim folderName As String
        Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Dim fileName As String
        Dim fInfo As System.IO.FileInfo
        Dim fCreation As Date
        Dim cfgLine As String()
        Dim numberOfDays As Integer

        sr = New StreamReader("c:\DeleteOld.cfg")
        Do
            cfgLine = Split(sr.ReadLine(), "|", 2)
            folderName = cfgLine(0)
            numberOfDays = Int(cfgLine(1))
            files = My.Computer.FileSystem.GetFiles(folderName, _
                        FileIO.SearchOption.SearchAllSubDirectories)
            For Each fileName In files
                fInfo = My.Computer.FileSystem.GetFileInfo(fileName)
                fCreation = fInfo.CreationTime
                If DateDiff(DateInterval.Day, fCreation, Now()) > numberOfDays Then
                    My.Computer.FileSystem.DeleteFile(fileName)
                End If
            Next

        Loop Until folderName Is Nothing
        sr.Close()

    End Sub

End Module

Similar Posts:

    None Found




No Comments


You can leave the first : )