Delete files in all subfolders

GDM69

Board Regular
Joined
Dec 21, 2011
Messages
50
Hi Folks:

How can I delete all *.txt files in the main folder C:\Gennex and all its subfolders (several layers). I use excel 2003.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Why don't you search .txt in windows explorer when you're open to that folder then highlight all results and delete them?
 
Upvote 0
Hello GDM69,

There are 2 macro here. One checks all the files in the parent folder and subfolders. If the file has a ".txt" extension then it is deleted.

The second macro "Run" starts the first macro and supplies it with the parent folder path. Here is the code.
Code:
' Thread:  http://www.mrexcel.com/forum/showthread.php?t=601024
' Poster:  GDM69
' Written: December 23, 2011
' Author:  Leith Ross

Sub DeleteTextFiles(ByVal FolderPath As String)

    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 LCase(File.Name) Like "*.txt" Then
                       File.Delete
                    End If
                Next File
                
                For Each SubFolder In Folder.SubFolders
                    DeleteTextFiles SubFolder
                Next SubFolder
                
End Sub

Sub Run()

    DeleteTextFiles "C:\Gennex"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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