Question : Auto save and zip macro that overwrites zipped files when condition is met

Hi Experts,

I have a macro that creates a backup of a file by saving it, then zipping it and moving it into a folder.  When the workbook is open this macro is automatically runs every 90 minutes.  The workbook is used a lot so as you can imaging I end up with heaps of backups.

What I want to be able to do add something to this macro the after a certain amount of time either deletes or overwrites the backups.  Specifically:

1. When backups get 2 weeks old they are delete or overwritten, BUT I still want to keep one from every day until
2. When backups get 6 months old I only only want to keep 1 from every week until
3. When backups get 24 months old I only want to keep 1 from every month.

Hope that makes sense.

Thanks.

 
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
Sub Zip_ThisWorkbook()
    Dim strDate As String, StrDateBUpFolder As String, DefPath As String, DefPathBackup As String
    Dim FileNameOriginZip, FileNameXls, FileNameDestinationZip, FileNameBUp
    Dim oApp As Object
 
 
    Application.DisplayStatusBar = True 'turns on the status bar
    Application.StatusBar = "Please wait while File is Saved and Zipped" ' displays a message in Status Bar
 
    Application.ScreenUpdating = False  ' Hides changes to screen until Macro finished
 
    'Define This workbook Path and Backup Folder date to use
    DefPath = ThisWorkbook.Path & "\"
    StrDateBUpFolder = Format(Now, "mmmyyyy")
  
    'Checks to See If A Directory Exists, If Not, Creates It
    DefPathBackup = ThisWorkbook.Path & "\" & "Enrol Backup" & StrDateBUpFolder & "\"
    DirTest = Dir$(DefPathBackup, vbDirectory)
        If DirTest = "" Then
            MkDir DefPathBackup
            DoEvents    'just to make sure it is there
        End If
        
   'Create date/time string and the temporary xls and zip file name
    strDate = Format(Now, " (ddd dmmmyy hmmam/pmss") & "sec)"
    FileNameBUp = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
    FileNameDestinationZip = DefPathBackup & FileNameBUp 'Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
    FileNameOriginZip = DefPath & FileNameBUp 'Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & strDate & ".zip"
    FileNameXls = DefPath & ThisWorkbook.Name
  
    If Dir(FileNameOriginZip) = "" Then ' And Dir(FileNameXls) = "" Then
        
        'Make copy of the Thisworkbook
        ThisWorkbook.Save
 
        'Create empty Zip File
        Application.ScreenUpdating = False  ' Hides changes to screen until Macro finished
        NewZip (FileNameOriginZip)
 
        'Copy the file in the compressed folder
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameOriginZip).CopyHere FileNameXls
 
        'Keep script waiting until Compressing is done
        On Error Resume Next
        Do Until oApp.Namespace(FileNameOriginZip).items.Count = 1
            Application.Wait (Now + TimeValue("0:00:01"))
        Loop
        On Error GoTo 0
 
        ' Move Zipped file to Backup Directory
        Name FileNameOriginZip As FileNameDestinationZip
        
        Else
    End If
    
    MsgBox "This file has been Saved and Zipped - Om Tat Sat"
    
    Application.StatusBar = False 'turns off Message
    Application.ScreenUpdating = True ' Turns back on screen updating
    
End Sub
 
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
    If Len(Dir(sPath)) > 0 Then Kill sPath
    Open sPath For Output As #1
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
    Close #1
End Sub

Answer : Auto save and zip macro that overwrites zipped files when condition is met

Ok...that isn't going to work...here is another macro...you have to add the microsoft scripting runtime reference for this to work...(Tools > References in the vb editor)
This code will delete any zip files older than 14 days old...you can change that to however many days you want...it is commented in the code..

Call the sub like this:

DeleteFiles "c:\albert"
where "C:\albert" is the path to your files

here is the code...I will also upload a new excel file...


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Public Sub DeleteFiles(FolderPath As String)
Dim EachFile As String
Dim Counter As Long
Dim FileList As Collection
 
Dim n As Integer
Dim Folder As Object
 
 
Dim o As FileSystemObject
Dim ofile As File
 
Set o = New FileSystemObject
Set Folder = o.GetFolder(FolderPath)
Set FileList = New Collection
 
 
 
    EachFile = Dir(FolderPath & "\*.zip") 'change zip to whatever type of file you want to include or make it an * to include all files
    Do While EachFile <> ""   ' Start the loop.
        Set ofile = o.GetFile(FolderPath & "\" & EachFile)
            FileList.Add ofile.DateCreated & "|" & EachFile, EachFile
        EachFile = Dir
    Loop
 
For Counter = 1 To FileList.Count
    'In the following line, any file older than 14 days will get deleted...change 14 to whatever you want
    If DateValue(Left(FileList(Counter), InStr(FileList(Counter), "|") - 1)) < DateValue(Now()) - 14 Then
        Kill FolderPath & "\" & Right(FileList(Counter), Len(FileList(Counter)) - InStr(FileList(Counter), "|"))
'        MsgBox FolderPath & "\" & Right(FileList(Counter), Len(FileList(Counter)) - InStr(FileList(Counter), "|"))
    End If
Next Counter
 
End Sub
 
In this file, run the macro 'DeleteOldFiles' but change the path first
 
Random Solutions  
 
programming4us programming4us