Or....to extend on that...this code deletes all files and folders within your specified folder:
Sub Clear_All_Files_And_SubFol
'Delete all files and subfolders
'Be sure that no file is open in the folder
Dim FSO As Object
Dim MyPath As String
Set FSO = CreateObject("scripting.fi
MyPath = "C:\Test" '<< Change
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If
If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " doesn't exist"
Exit Sub
End If
On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.*", True
'Delete subfolders
FSO.deletefolder MyPath & "\*.*", True
On Error GoTo 0
End Sub
Main Topics
Browse All Topics





by: geodan7Posted on 2007-06-13 at 13:40:04ID: 19277941
The following will delete all the files in the folder...and then...once empty, it can delete the folder.
Sub DeleteExample4()
'You can use this to delete the whole folder
'Note: RmDir delete only a empty folder
On Error Resume Next
Kill "C:\Test\*.*" ' delete all files in the folder
RmDir "C:\Test\" ' delete folder
On Error GoTo 0
End Sub