Delete Old Files (dotNet)

Posted on March 2nd, 2007 in Coding, Work by abdallah

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.

[ vb Code ]
  1.  
  2. ‘ Written by Abdallah Deeb on 02-Mar-2007
  3. ‘ Use at your own risk, I am not liable for any damages or lost files if you use this code.
  4. Imports System
  5. Imports System.IO
  6. Module DeleteOld
  7.  
  8.     Sub Main()
  9.         Dim sr As StreamReader
  10.         Dim folderName As String
  11.         Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
  12.         Dim fileName As String
  13.         Dim fInfo As System.IO.FileInfo
  14.         Dim fCreation As Date
  15.         Dim cfgLine As String()
  16.         Dim numberOfDays As Integer
  17.  
  18.         sr = New StreamReader("c:\DeleteOld.cfg")
  19.         Do
  20.             cfgLine = Split(sr.ReadLine(), "|", 2)
  21.             folderName = cfgLine(0)
  22.             numberOfDays = Int(cfgLine(1))
  23.             files = My.Computer.FileSystem.GetFiles(folderName, _
  24.                         FileIO.SearchOption.SearchAllSubDirectories)
  25.             For Each fileName In files
  26.                 fInfo = My.Computer.FileSystem.GetFileInfo(fileName)
  27.                 fCreation = fInfo.CreationTime
  28.                 If DateDiff(DateInterval.Day, fCreation, Now()) > numberOfDays Then
  29.                     My.Computer.FileSystem.DeleteFile(fileName)
  30.                 End If
  31.             Next
  32.  
  33.         Loop Until folderName Is Nothing
  34.         sr.Close()
  35.  
  36.     End Sub
  37.  
  38. End Module

Post a comment

You must be logged in to post a comment.