Deleting old folder from a directory (backup)...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have had a script running to make a daily backup of files in a directory. The files are all bundled into a folder and saved into the backup directory (as a backup system)

The files are all tiny, but I have noticed that my 'backup directory' is growing ever bigger and I only really need to keep 7 days worth of backups...

I can do a count of the folders in the backup directory, but if the count is greater than 7, I need to identify the 'oldest' folder and kill it (or any oldest folders after the count of 7)

Is this possible?

If you can point me in the right direction, I'd be eternally grateful

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Thanks for that RedBeard. I'm not really sure where to go with that. I found something similar where it deletes 'files' which are older than 7 days:

Code:
Sub DeleteOldFiles() 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    For Each fcount In FSO.GetFolder(ThisWorkbook.Path & "\Backups\ ").Files 
        If DateDiff("d", fcount.DateCreated, Now()) >7 Then 
            Kill fcount 
        End If 
    Next fcount 
End Sub
I was thinking last night. All of these folders in the directory are named by the date they were created:

Code:
Dim newFolderName As String
    newFolderName = Format(Date, "yyyymmdd")
Could I somehow cycle through the names and delete the last one as they will all be in order already? Something like:

Code:
    Dim numberoffolders As Integer
    numberoffolders = subfolders.Count
    'MsgBox numberoffolders
    
    If numberoffolders > 7 Then
         'delete lastfolder
    End If
I'd really like to keep 7 folders of backups, rather than 7 days worth. If the program isn't run in a week, the first code (although referring to files rather than folders) will delete all the backups. If I keep 7 folders behind, then at least I will have enough backups for safety... if you get my drift...
 
Last edited:
Upvote 0
Excel being annoying I did this in powershell instead:

Code:
Get-ChildItem c:\temp\test |
Where-Object { $_.PSIsContainer } |
Sort-Object CreationTime -Descending |
Select-Object -Skip 7 |
Remove-Item -Recurse -Force

You'd have to replace the path C:\tempt\test with your actual location.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top