Delete File by Creation Date

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am currently using the code below within the "this workbook" section to save a file to one location and also create a backup copy of a file to a different location adding the date to the end of the filename. "filename dd/mm/yy.xls"

Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs "C:\Folder Location\Back Up Folder" & "Filename Backup " & Format(Now(), "dd-mm-yy") & ".xls")
ActiveWorkbook.Save
Application.DisplayAlerts = True


This of course generates a new file every day.

What I would like to do is add a little more code to it so that it deletes back up copies of the file which are more than two days old.

ie - if saved on the 5th of the month ("filename 05/mm/yy") it would delete the file called "filename 03/mm/yy" (and anything before) leaving just the filenames with 4th and 5th in the name and so on rolling along daily.

I have tried several examples on here but none seem to work. Can someone please assist.
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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