Delete files in folder; keep last file of each day

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I create a timestamped backup of my Personal.xlsb (filename & "_yyyymmdd_hhss") everytime I save it. As you can imagine, this leads to a lot of versions each day. Sometimes I need to go back a few hours within the day, so creating the backups using the date only doesn't work.

Could someone give me some pointers how I might loop thru all files within a specified folder, and deleting all files EXCEPT the last saved file of each individual day. I'd likely run this once a week, so I keep the number of files manageable.

For example, if the following files were in a folder:

Personal.xlsb_20160914_1023.bak
Personal.xlsb_20160914_1700.bak
Personal.xlsb_20160915_1156.bak
Personal.xlsb_20160915_1444.bak
Personal.xlsb_20160915_1603.bak

All files except the two below would get deleted:

Personal.xlsb_20160914_1700.bak
Personal.xlsb_20160915_1603.bak

Thanks y'all.
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I think I would use something like a Workbook_BeforeSave event.
Code:
Private Sub Workbook_BeforeSave(Cancel As Boolean)
Dim myPath As String, fName As String, dt As String
myPath = ThisWorkbook.Path
dt = Format(Date, "yyyymmdd")
If Right(myPath, 1) <> "\" Then myPath = myPath & "\"
fName = Dir(myPath & "*.*" & dt & "*.bak")
    Do While fName <> ""
        If Right(fName, 4) < Right(ThisWorkbook.Name, 4) Then Kill fName
        fName = Dir
    Loop
End If
End Sub
The code is untested.
 
Upvote 0
JLGWhiz,

Much appreciated. However, I'm not looking to immediately delete all files at the end of each day; instead, I would run this once a week. Given this, the sub would need to cycle thru each individual date, identify the last time for each date, and delete (or move; probably safer than outright deleting) the other files within that date.

I would imagine that it would consist of two loops - the outside loop to cycle thru the dates and the inside loop to cycle thru the times within that date. Would the inner loop use Step -1 (start at the last time, and move/delete any time less than the daily max time)? I just can't figure out how to construct loops that look in the middle of a string and evaluate/discriminate.

Thanks much.
 
Upvote 0
To fill in the blanks, I thought I'd post a solution from Marc L (Chandoo.org)

Code:
Sub Demo()
         Dim SP$, TE$, V
     With CreateObject("Scripting.Dictionary")
         For Each V In [{"Personal.xlsb_20160914_1700.bak","Personal.xlsb_20160914_1023.bak","Personal.xlsb_20160915_1156.bak","Personal.xlsb_20160915_1444.bak","Personal.xlsb_20160915_1603.bak"}]
                    SP = Split(V, "_")(1)
                    TE = .Item(SP)
             If V > TE Then
                 .Item(SP) = V
                 If TE > "" Then Debug.Print TE; " : moved"
             Else
                 Debug.Print V; " : moved"
             End If
         Next
              Debug.Print vbLf; "The"; .Count; "last files :"; vbLf; Join(.Items, vbLf)
             .RemoveAll
     End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,592
Members
449,174
Latest member
chandan4057

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