CODE VBA DELETE ALL FILES AND FOLDERS

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone. I need a code to delete all folders and files, what should I do. Currently I am using below code which deletes all files only. The folder cannot be deleted.
Sincerely thank

1632749314237.png


VBA Code:
Sub Remove_File()
 Kill "C:\Users\PC\Desktop\Test\*.*"
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Wouldn't rather have the files sent to the recycling bin? You're aware that there is no Undo for this - that you cannot CTRL-Z yourself to safety if anything goes wrong...?
 
Upvote 0
Hi Excelpromax123
this code does exactly what you ask (with irreversible results)

VBA Code:
Sub DeleteFolder()
    
    Dim FSO As Object
    Dim MyPath As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    MyPath = "C:\Users\PC\Desktop\Test"        '<< To adapt if needed
    
    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If
    
    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & "        't exist"
        Exit Sub
    End If
    
    FSO.DeleteFolder MyPath
    
End Sub
 
Upvote 0
Solution
Hi Excelpromax123
this code does exactly what you ask (with irreversible results)

VBA Code:
Sub DeleteFolder()
   
    Dim FSO As Object
    Dim MyPath As String
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
   
    MyPath = "C:\Users\PC\Desktop\Test"        '<< To adapt if needed
   
    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If
   
    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & "        't exist"
        Exit Sub
    End If
   
    FSO.DeleteFolder MyPath
   
End Sub
Thanks, buddy. The code runs very well
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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