Keeping backup files that is only 2 days old and delete the rest

  • I have files coming in from my backup to my drive and I was asked to clean up any files that is more than 2 days old. My current dir structure look like the below.

    D:\Client\ServerName\Backups\database1.bak

    I have like more than 10 of these right now. (i am generating 2 everyday). How can I:

    1. keep files those are only 2 days old

    2. be able to delete until the 3 level in the subfolder(refer to the code below)

    3. Automate all this[/b]

    Here is the current code for 1 level of subfolder:-

    iDaysOld = 2

    strPath = "D:\Client\ServerName\Backups\database1.bak\"

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFolder = objFSO.GetFolder(strPath)

    Set colSubfolders = objFolder.Subfolders

    Set colFiles = objFolder.Files

    For Each objFile in colFiles

    If objFile.DateCreated < (Date() - iDaysOld) Then

    'MsgBox "Dir: " & objFolder.Name & vbCrLf & "File: " & objFile.Name

    objFile.Delete

    End If

    Next

    For Each objSubfolder in colSubfolders

    Set colFiles = objSubfolder.Files

    For Each objFile in colFiles

    If objFile.DateCreated < (Date() - iDaysOld) Then

    'MsgBox "Dir: " & objSubfolder.Name & vbCrLf & "File: " & objFile.Name

    objFile.Delete

    End If

    Next

    Next

    [font="Verdana"]Imagination is more important than knowledge-Albert Einstein[/font]

  • why not just allow the sql maint plan or job to delete files after N days

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • WOW, HOW DO YOU THAT?

    [font="Verdana"]Imagination is more important than knowledge-Albert Einstein[/font]

  • SQL King (7/29/2008)


    WOW, HOW DO YOU THAT?

    eek now i'm getting worried 😀

    how are you backing up the databases?

    Via a maint plan or a custom job or maunal T-SQL script, etc?

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • use a maintanence plan for clean up tasks

    Under Management --> Maintenance Plans..

  • Sorry, maybe I asked you the wrong way.

    I know that it is in the management studio and maintenance plan task and all that. But I have those files in my D drive, not in a SQL table, how to achieve that?

    [font="Verdana"]Imagination is more important than knowledge-Albert Einstein[/font]

  • Back ups are done thru some other third party tool. I am not actually responsible for that. But from our side of the team, we just want to have the most recent files(2 days old) and delete the rest. I must be able to delete a file from my D drive and this must be automated.

    [font="Verdana"]Imagination is more important than knowledge-Albert Einstein[/font]

  • so your backing up via a maintenance plan are you?

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Under the maintenance clean up task,

    you can search a folder and delete based on the extension.

    Enter the UNC path for your folder on your D drive..

    Automate this task to run daily and delete any files that are older than 3 days..

  • Great! working, never knew that was so easy. Thanks all!

    [font="Verdana"]Imagination is more important than knowledge-Albert Einstein[/font]

  • your welcome 😉

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply