Remove directory and it's contents (files, subdirectories) without using FileSystemObject

前端 未结 3 2004
醉梦人生
醉梦人生 2020-12-11 17:44

I want to know if it\'s possible to rewrite this piece of code:

Private Sub PrepareDir(ByVal dir As String)
    Dim fso As New FileSystemObject
    If fso.Fo         


        
3条回答
  •  执笔经年
    2020-12-11 18:19

    No need to delete files for Deleting folders. Take the path and search for the sub folders in a loop and that sub folder can be deleted. below is the example :copy both procedures and paste on module

    Public Function Delete_Folder(ByVal FldrName As String) Dim fso, FSfolder As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set FSfolder = fso.GetFolder(Application.DefaultFilePath)' This is My Documents folder path
    

    'You can replace with your original folder path

    For Each Folder In FSfolder.SubFolders
        'Debug.Print Folder.Name
        If Folder.Name = FldrName Then
            Folder.Delete
            Exit For
        End If
    Next
    

    End Function

    Sub test() Delete_Folder "Sub_Folder_Name" End Sub

提交回复
热议问题