Monday, October 22, 2012

How to Delete all Files and Sub Folders In a Folder using VBA

VBA Macro To Delete all Files and Sub Folders 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

Thanks,Tamatam

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog