Macro to delete backup files in folder and sub-folder

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I would like to to delete all back files eg Backup TB in folder TB as well as sub-folders
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Thanks for the help, much Appreciated

I have amended the path , but set it up to delete only the files starting with thename backup, but cannot get it to work

Code:
 Sub Clear_All_Files_SubFolders_In_Folder()

     Dim FSO As Object
    Dim MyPath As String

     Set FSO = CreateObject("Scripting.FileSystemObject")

 MyPath = "C:\pull\"

     If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If

     If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " Folder Doesn't Exist"
        Exit Sub
    End If

     On Error Resume Next
    'Delete Files
    FSO.DeleteFile MyPath & "\Backup.*", True
    'Delete SubFolders
    FSO.DeleteFolder MyPath & "\backup.*", True

 End Sub
 
Upvote 0
Give me more details on what did not work. I created files and folders to match you code and it ran for me.

I was not aware that you wanted to delete certain filenames. The code I linked will delete all files and subfolders in the provided path. The way you changed it is the DeleteFile line will delete files with the filename "Backup" no matter the extension and DeleteFolder will delete the whole subfolder called "backup" no matter what the containing filenames and extensions are. Is that what you wanted?

Since you are deleting files and folders, make sure you test on sample files/folders.
 
Upvote 0
I have created a test folder called "Test" and copied the workbooks and folders to this folder

When I run the macro, no errors occurs. I delete the sub-folders within the test folder.

I want the macro to do it to delete all .xls files starting with the name backup in the folder "test" as well as its sub-directories

Your assistance in this regard is most appreciated

Code:
 Sub Clear_All_Files_SubFolders_In_Folder()

     Dim FSO As Object
    Dim MyPath As String

     Set FSO = CreateObject("Scripting.FileSystemObject")

 MyPath = "C:\test\"

     If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If

     If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " Folder Doesn't Exist"
        Exit Sub
    End If

     On Error Resume Next
    'Delete Files
    FSO.DeleteFile MyPath & "\*Backup.*", True
    Delete SubFolders
    FSO.DeleteFolder MyPath & "\*.*", True

 End Sub
 
Last edited:
Upvote 0
Try this slightly modified version I found by Leith Ross. My test had about 20 subfolders. Make sure you test this on sample files and subfolders.

Code:
Option Explicit
Option Compare Text

Sub DeleteBackupFiles(ByVal FolderPath As String)
    ' Author:  Leith Ross
    Dim File As Object
    Dim Folder As Object
    Dim FSO As Object
    Dim SubFolder As Object
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder(FolderPath)
            
    For Each File In Folder.Files
        If File.Name Like "*backup.*" Then
           File.Delete
        End If
    Next File
                
    For Each SubFolder In Folder.SubFolders
        DeleteBackupFiles SubFolder
    Next SubFolder
                
End Sub

Sub Run()
    DeleteBackupFiles "C:\test"
End Sub
 
Upvote 0
Thanks for the help. Where do paste this code in order to run the code?
 
Upvote 0
Hi..

Try this one..

Code:
Sub Delete()
    Dim x, fldr As FileDialog, SelFold As String, i As Long
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    
    'Select Folder
    With fldr
        .Title = "Select a Folder"
        If .Show <> -1 Then Exit Sub
        SelFold = .SelectedItems(1)
    End With
    
    'Put all required filesinto an array
    x = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & SelFold & "\Backup*.xls"" /s/b").stdout.readall, vbCrLf)
    
    'Loop through the array and delete each file
    For i = LBound(x) To UBound(x) - 1
        CreateObject("scripting.filesystemobject").DeleteFile x(i)
    Next i
    
    'Show Result
    MsgBox "The Following Files Were Deleted:" & vbLf & vbLf & Join(x, vbLf)
End Sub
 
Upvote 0
Thanks it works perfectly.

If not too much trouble, would you kindly amend so that all files starting with the name "Backup" in C:\test and it sub-folders are deleted instead if having to select each folder where one want to delete these workbooks
 
Upvote 0
For my code, just paste it into a module and run the macro called "run". Make sure you test this on sample files and subfolders.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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