Click here to Skip to main content
Licence CPOL
First Posted 7 Nov 2009
Views 7,371
Downloads 170
Bookmarked 11 times

File (Backup, Log,.... ) Rotator

By | 7 Nov 2009 | Article
A useful script for clearing old backup files and making space for new ones.

Introduction

This article introduces a method to delete old backup or log files from folders in order to prevent waste of disk space.

Background

As a system administrator, you may want to check the folders which contain backup or log files. Any backup method you are using generates several files, for example, daily files. If you don't clear these folders from time to time, it will fill up your disk space after some time.

There should be an effective way to delete old files automatically. Some platforms like Linux offer the Log Rotation daemon which automatically removes files at certain times and deletes old files. Since in Windows there is no specific way to do this, you should modify your scripts to generate separate backup or log files, and use the script I'm going to present here to keep a certain number of new files and delete the older files.

Using the Code

Suppose you have a folder containing multiple files and you want to keep, for example, the four newest files and delete the others.

If the folder contains these files:

  • log_20091101
  • log_20091102
  • log_20091103
  • log_20091104
  • log_20091105
  • log_20091106

The script reads the last modified date of the files and sorts them and deletes the older files:

  • log_20091105
  • log_20091106

For this purpose, put this VBScript code somewhere in your hard drive and create a batch file in the same folder as the script, which runs this script. You need to run the batch file automatically using Windows Scheduled Task.

'This scripts deletes the old files in the [Path] and keeps the [MaxFiles] files.

Path = WScript.Arguments.Item(0)
MaxFiles=WScript.Arguments.Item(1)

Set fs = WScript.CreateObject ("Scripting.FileSystemObject")
Set ContentFolder = fs.GetFolder(Path)
Set Files = ContentFolder.Files

Dim FileNames(100)
Dim ModifiedDates(100)

Dim TempDate
Dim TempName
Dim Count
Dim i
Dim j

Count = Files.Count

i=0
For Each File in Files

    FileName = File.Name
    ModifiedDate = File.DateLastModified
    

    FileNames(i)     = FileName
    ModifiedDates(i) = ModifiedDate

	i=i+1
Next

For i=0 to Count-1
    For j=0 to i-1
    	If ModifiedDates(i) > ModifiedDates(j) Then

            TempDate = ModifiedDates(i)
            TempName = FileNames(i)
            
            ModifiedDates(i) = ModifiedDates(j)
            FileNames(i) = FileNames(j)
            
            ModifiedDates(j) = TempDate
            FileNames(j) = TempName
        
    	End If
    Next
Next

For i=MaxFiles to Count-1
    FullPath= Path & "\\" & FileNames(i)
    fs.DeleteFile(FullPath)
Next

The batch file will run periodically to delete the old files in the your folders.

The first command keeps the five newest files in Your_Folder_1, and the second command keeps the seven newest files and delete the old ones.

wscript Rotator.vbs Your_Folder_1 5
wscript Rotator.vbs Your_Folder_2 7

Points of Interest

Actually, it was a bad idea to delete the files older than, for example, seven days. Because if your backup method scripts fail to run for seven days and the Rotator script ran everyday, you would face an empty folder after seven days, which is not desirable.

Instead, the idea of keeping the seven new files is not risky even if the backup methods fail. So after seven days, at least you will have the old backups intact, and you will not be blamed for destroying data. So running this script is completely safe.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vahab Shalchian

Software Developer
Arya Hamrah Samaneh Co.
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

Working on both Java and .NET Technology ,developing Enterprise applications, Mobile Applications.
Worked as a Senior Developer at SAM Enterprise company which resides in Tehran, Iran, and is a partner with S4M GmbH which develops solutions for media and is a Microsoft Gold Partner.
Now is working as a developer in Arya Hamrah Samaneh Co. on ICT projects from Irancell/MTN, the 2nd GSM operator in Tehran.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIs the variable assignment necessary? Pinmemberbrainfried23:51 15 Dec '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 8 Nov 2009
Article Copyright 2009 by Vahab Shalchian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid