VBA – Delete all files in a folder

This code snippet shows how you can delete all files in a given folder in a VBA application.

Sub deleteFiles()
dim myPath
myFolder = "C:\MyFolder1\Myfolder2"      
Set Fso = CreateObject("Scripting.FileSystemObject") ' Get a File object to query.
Set Fldr = Fso.GetFolder(myFolder)
                
For Each Filename In Fldr.Files
    Filename.Delete True ' delete all files
Next
End Sub

That’s it!

Wednesday, December 8th, 2010 VBA

No comments yet.

Leave a comment


7 − five =