Excellent VBA to delete old files SOLVED!!

Sven62

Active Member
Joined
Feb 21, 2012
Messages
485
Here you go! You can thank some dude over at Ozgrid. Was posted in Jan 2012. Just replace the path and how many days old the files you want to delete. It will delete ALL files in that folder that are too old.

On a side note... can someone please tell me what that little circle is before "FSO" in the second code? I've never seen those before.

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

AND..

Code:
Sub DelOldFiles() 
     
     
     ''Clear out all files over 7 days old from Dir_Path folder.
     '
    Dir_Path = "C:\Folder\SubFolder\" 
    iMaxAge = 7 ' Set the number of days
     
    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    If oFSO.FolderExists(Dir_Path) Then 'Check that the folder exists
        For Each oFile In oFSO.GetFolder(Dir_Path).Files 
            If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then 'Look at each file to check if it is older than 7 days
                oFile.Delete 
            End If 
        Next 
    End If 

    End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
FSO is a variable name, you can rename it john
Code:
Set john = CreateObject("Scripting.FileSystemObject") 
For Each fcount In john.GetFolder(ThisWorkbook.Path & "\Backups\ ").Files
 
Upvote 0
FSO is a variable name, you can rename it john
Code:
Set john = CreateObject("Scripting.FileSystemObject") 
For Each fcount In john.GetFolder(ThisWorkbook.Path & "\Backups\ ").Files


No.... what is the little circle? In the second code example.
 
Last edited:
Upvote 0
The letter o? as in m n o. The name of the variable is oh-EF-ES-OH.

Here you go! You can thank some dude over at Ozgrid. Was posted in Jan 2012. Just replace the path and how many days old the files you want to delete. It will delete ALL files in that folder that are too old.

On a side note... can someone please tell me what that little circle is before "FSO" in the second code? I've never seen those before.

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

AND..

Code:
Sub DelOldFiles() 
     
     
     ''Clear out all files over 7 days old from Dir_Path folder.
     '
    Dir_Path = "C:\Folder\SubFolder\" 
    iMaxAge = 7 ' Set the number of days
     
    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    If oFSO.FolderExists(Dir_Path) Then 'Check that the folder exists
        For Each oFile In oFSO.GetFolder(Dir_Path).Files 
            If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then 'Look at each file to check if it is older than 7 days
                oFile.Delete 
            End If 
        Next 
    End If 

    End Sub
 
Upvote 0
This is interesting, but what is the difference between the two codes?
One goes from the date last modified, the other from the date created.
One deletes the file (sends to recycle bin), the other kills the file (bypasses the recycle bin)
One uses the current workbook path and the other specifies a path.

No.... what is the little circle? In the second code example.
The small o is normally just a hint to the code writer to remind them that they are dealing with an object. As Patel45 stated you can call it what you like. So FSO, oFSO and john(in Patel45's code) are all the same.
 
Upvote 0
OK guys... after all this time the script has been running fine.... until... Bill Gates broke it. Only on SOME of our computers. I get a "Object not recognized in Module 10" or words to that effect. I am assuming those computers cannot deal with the
Code:
[COLOR=#333333][I]CreateObject("Scripting.FileSystemObject")[/I][/COLOR]
. Any solutions?

Full code is
Code:
[COLOR=#333333][I]Sub DeleteOldFiles() [/I][/COLOR]    
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  [COLOR=#333333][I]End Sub[/I][/COLOR]

Office 2010 on all computers.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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