macro to delete all files in a folder except xlsm,and csv

RAM1972

Board Regular
Joined
Jun 29, 2014
Messages
217
On my desktop a folder named data, how to delete all files with extension txt ,xlsx,xls but not to delete xlsm and csv with vba.
There 1000 files in that folder
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try the macro given below but, be aware that the files will be deleted from the selected folder and you will not be able to recover them from Recyle Bin.

So, first try the code with a test folder containing some sample files.

Code:
Sub Test()
    Set objFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select Folder !", 0)
    If Not objFolder Is Nothing Then
        MyPath = objFolder.Items.Item.Path
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set MyFolder = FSO.GetFolder(MyPath)
        Set MyFiles = MyFolder.Files
        
        For Each File In MyFiles
            FileExt = FSO.GetExtensionname(File)
            If FileExt = "txt" Or FileExt = "xlsx" Or FileExt = "xls" Then File.Delete
        Next
    End If
    
    Set FSO = Nothing
    Set MyFiles = Nothing
    Set MyFolder = Nothing
End Sub
 
Upvote 0
Hi

Thank you a lot :cool: works as requested, need a little tuning , I have three static folders named data_1, data _2, and data_3 on my desktop.

How to adjust the code without asking which folder to perform action.


What I mean go directly to these folders and delete the requested extension
 
Upvote 0
So, here is the final code.

You may need to to do just a fine tuning to change the names of your folders Data_1, Data_2 and Data_3 if needed.

Code:
Sub Main()
    Dim WshShell As Object
    Dim Folder1 As String, Folder2 As String, Folder3 As String
    
    Folder1 = "Data_1"
    Folder2 = "Data_2"
    Folder3 = "Data_3"
    
    Set WshShell = CreateObject("WScript.Shell")
    DesktopPath = WshShell.SpecialFolders("Desktop")
    
    Call Clean_Folders(DesktopPath & Application.PathSeparator & Folder1)
    Call Clean_Folders(DesktopPath & Application.PathSeparator & Folder2)
    Call Clean_Folders(DesktopPath & Application.PathSeparator & Folder3)

    Set WshShell = Nothing
End Sub
'
Sub Clean_Folders(MyPath As String)
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = FSO.GetFolder(MyPath)
    Set MyFiles = MyFolder.Files
    
    For Each File In MyFiles
        FileExt = FSO.GetExtensionname(File)
        If FileExt = "txt" Or FileExt = "xlsx" Or FileExt = "xls" Then File.Delete
    Next
    
    Set MyFiles = Nothing
    Set MyFolder = Nothing
    Set FSO = Nothing
End Sub
 
Upvote 0
Hello Haluk

As post 4,an insight has happened .

now I have have the following the path C:\\ User\User5\Desktop.

How to delete same on the desktop and all folders on the desktop for txt, xlsx,and xls.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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