t0ny84
Board Regular
- Joined
- Jul 6, 2020
- Messages
- 205
- Office Version
- 365
- 2016
- 2013
- Platform
- Windows
- Mobile
- Web
Hi,
I have written the below code for a previous spreadsheet I built and am hoping someone can assist in how I can change this to allow it to check in a main directory (including any sub directories) for XLSX Files and if older than 6 months and have BACKUP in the name delete (the backup folder and the sub directories SHOULD only have the back up files in there no other files).
The below code currently copies a set sheet from a workbook into a brand new spreadsheet then saves the document in a folder based on the year (2021, 2022, etc. C:\Backup\YEAR FOLDER) using a file name format of "backup_MONTH_dd_mm_yy_ss.xlsx". What I am trying to add on is when the script runs (either by another script or as part of this script) it checks the all XLSX files in the folder BACKUP and any sub folders (e.g. 2021) to see if the date portion of the file name is over 6 months old. If over 6 months then it would either move to a specified folder or delete these backups.
The check I believe is the easiest to use (as already using FileSystemObject) would be something like:
Example
trimmedfilename = Trim(fso.Filename)
If trimmedfilename is less than (trimmeddfilename + 180 (6 months (30x6))) then keep the file.
If over trimmedfilename + 180 (over 6 months old) it would either delete these files or move them into another sub directory called delete.
If I can get the files deleted without user confirmation ok oherwise would move them to that directory (e.g. Over 6 Months Old) and I would manually delete them when needed.
e.g.
get fso filename trimmed
If filename (date) = 6 months old delete else skip
next file name
if ws.filename'
Please don't hesitate to ask further questions to get a better understanding! and as always thanks in advance!
t0ny84
I have written the below code for a previous spreadsheet I built and am hoping someone can assist in how I can change this to allow it to check in a main directory (including any sub directories) for XLSX Files and if older than 6 months and have BACKUP in the name delete (the backup folder and the sub directories SHOULD only have the back up files in there no other files).
The below code currently copies a set sheet from a workbook into a brand new spreadsheet then saves the document in a folder based on the year (2021, 2022, etc. C:\Backup\YEAR FOLDER) using a file name format of "backup_MONTH_dd_mm_yy_ss.xlsx". What I am trying to add on is when the script runs (either by another script or as part of this script) it checks the all XLSX files in the folder BACKUP and any sub folders (e.g. 2021) to see if the date portion of the file name is over 6 months old. If over 6 months then it would either move to a specified folder or delete these backups.
The check I believe is the easiest to use (as already using FileSystemObject) would be something like:
Example
trimmedfilename = Trim(fso.Filename)
If trimmedfilename is less than (trimmeddfilename + 180 (6 months (30x6))) then keep the file.
If over trimmedfilename + 180 (over 6 months old) it would either delete these files or move them into another sub directory called delete.
If I can get the files deleted without user confirmation ok oherwise would move them to that directory (e.g. Over 6 Months Old) and I would manually delete them when needed.
e.g.
get fso filename trimmed
If filename (date) = 6 months old delete else skip
next file name
if ws.filename'
VBA Code:
Sub FileBackUp()
Dim srcSheet As Worksheet
Dim NewBook As Workbook
Dim wshape As Shapes
Dim fso As FileSystemObject
Dim FileExtension As String
Dim BackUpFolderLocation As String
Dim BackUpFileName As String
Dim FullBackUpFileName As String
Dim BackUpYearFolder As String
Dim uName As Variant
uName = Environ("USERNAME") * 1
BackUpFolderLocation = "C:\Backup\"
BackUpYearFolder = Format(Date, "yyyy")
BackUpFileName = "backup_" & Format(Now(), "mmmm_dd_mm_yy_ss")
Set srcSheet = Nothing
Set srcWBK = Nothing
Set NewBook = Nothing
Set fso = New FileSystemObject
If fso.FolderExists(BackUpFolderLocation) = False Then
fso.CreateFolder (BackUpFolderLocation)
Else
End If
If fso.FolderExists(BackUpFolderLocation) = True Then
' Part 2 of original copy method
'Set srcSheet = ThisWorkbook.Worksheets("TIL Logger")
'Set NewBook = Workbooks.Add
'srcSheet.Copy After:=NewBook.Sheets(Sheets.Count)
'Application.DisplayAlerts = False
Set NewBook = Workbooks.Add
ThisWorkbook.Sheets("TIL Logger").UsedRange.Copy
ActiveSheet.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
With NewBook
With .Sheets("Sheet1")
.Rows("1:8").EntireRow.Delete
.Range("M1:N1").Merge
.Range("M1:N1").VerticalAlignment = xlCenter
.Range("M1").Value = "Backed Up"
.Range("M2").Value = "Date:"
.Range("M3").Value = "Time:"
.Range("M4").Value = "Stafflink ID (by)"
.Range("N2").Value = Format(Date, "dd/mm/yyyy")
.Range("N3").Value = Format(Date, "hh:mm AM/PM")
.Range("N4").Value = uName
.SaveAs Filename:=BackUpFolderLocation & BackUpFileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End With
.Close
End With
End If
End Sub
Please don't hesitate to ask further questions to get a better understanding! and as always thanks in advance!
t0ny84