Technical Article

Delete Old backup Files

,

The following script deletes files in the specified folder, older than specified no of days and outputs the details to the specified file location. I use this VB script to delete some old backup files those can't be deleted with the regular maintenance plans. All you need to do is to supply the backup folder name and location, No of days and output file name and location.

'* Specify the folder Name & Location here 

Foldername="C:\Backup location\Folder_Name"

'* Specify how many days worth of Backup files you wanted to keep on the drive
   Days = 10

'* Specify the Output fiel Name & location 
   LogFileName= "C:\Backup Location\Folder_name\Outputfilename.txt"

   Counter = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set LogFile=objFSO.OpenTextFile(LogFileName,2,true)
LogFile.WriteBlankLines 1
LogFile.Writeline " **************************************************"
LogFile.Writeline " * Delete Biztalk Backup files Older than 10 Days *"
LogFile.Writeline " **************************************************"
LogFile.WriteBlankLines 1
LogFile.Writeline " Backup Folder Name .........: " & Foldername
LogFile.Writeline " Deleting files older then...: " & Days & " days"
LogFile.Writeline " Output File ................: " & LogFileName
LogFile.Writeline " Date Deleted ...............: " & Date
LogFile.WriteBlankLines 2

Set objFolder = objFSO.GetFolder(Foldername)
For Each file in objFolder.Files
   FileName=file.name
   FileFullName=Foldername & "\" & filename
   Set objFile = objFSO.GetFile(FileFullName)
   
   LastModifiedDate=objFile.DateLastModified
   IsOld=DateCheck(LastModifiedDate)
   If IsOld="old" then

    objFSO.DeleteFile(FileFullName)
        Counter = Counter + 1
LogFile.Writeline FileName & "," & LastModifiedDate
end if
   

   FileName=null
   FileFullName= null
   LastModifiedDate= null
   IsOld= null
   Set objFile = Nothing
next
   LogFile.WriteBlankLines 3
   LogFile.Writeline "Total Old file(s) Deleted.....: " & Counter
   LogFile.WriteBlankLines 3

LogFile.Close


Function DateCheck(Lastmodified)
   YToday=Year(date)
   MToday=Month(date)
   DToday=Day(date)

   YLastModified=Year(Lastmodified)
   MLastModified=Month(Lastmodified)
   DLastModified=Day(Lastmodified)
   LastmodifiedNum=YLastModified & MLastModified & DLastModified

   YExpiration=Year(DateSerial(Ytoday,Mtoday,DToday-Days))
   MExpiration=Month(DateSerial(Ytoday,Mtoday,DToday-Days))
   DExpiration=Day(DateSerial(Ytoday,Mtoday,DToday-Days))
   ExpirationNum=YExpiration & MExpiration & DExpiration

   If LastmodifiedNum < ExpirationNum then
      DateCheck="old"
   else
      DateCheck="new"
   end if
end Function

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating