Monday, 22 October 2012

How To Delete all Files and SubFolders In a Folder

Macro To Delete all Files and SubFolders In a Folder
'Be Sure That No File is Open in The Folder
Sub Clear_All_Files_SubFolders_In_Folder()

    Dim FSO As Object
    Dim MyPath As String

    Set FSO = CreateObject("Scripting.FileSystemObject")

MyPath = "C:\Documents and Settings\Administrator\My Documents\Sample"

    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If

    If FSO.FolderExists(MyPath) = False Then
        MsgBox MyPath & " Folder Doesn't Exist"
        Exit Sub
    End If

    On Error Resume Next
    'Delete Files
    FSO.DeleteFile MyPath & "\*.*", True
    'Delete SubFolders
    FSO.DeleteFolder MyPath & "\*.*", True

End Sub

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your open Feedback only related to this Blog Posts. Please note that I cannot respond to the Anonymous Comments.

Subscribe to Blog Posts by Email

ExcelKingdom-Popular Posts