Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    660

    Unanswered: Delete pdf file in all subfolder

    Please help, i would like to delete all the pdf file in a folder and their subfolder. How can i do this? Anybody have an idea? Many thanks.

  2. #2
    Join Date
    Dec 2005
    Posts
    45
    This will do all files in a single folder. Not sure about subfolders...

    Sub test()

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set d = fs.GetFolder("C:\test")

    For Each file In d.Files
    If Right(Trim(file.Name), 3) = "pdf" Then
    Debug.Print file.Name
    End If
    Next file

    End Sub

  3. #3
    Join Date
    Mar 2004
    Posts
    660
    But this is only work for the specified folder. Will it delele all the folder and their subfolder? Thank you very much!

  4. #4
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1

    Recursive file deleting procedure

    Hi

    I have been looking at the FileSystemObect in the last day so just knocked this up, which should do the trick.

    Code:
    Sub DeletePDFs(ByVal TopDirectory As String)
        Dim fSys As FileSystemObject
        Dim TopFolder As Folder
        Dim SubFldr As Folder
        Dim fl As File
        
        Set fSys = New FileSystemObject
        
        If fSys.FolderExists(TopDirectory) Then
            Set TopFolder = fSys.GetFolder(TopDirectory)
            For Each fl In TopFolder.Files
                If Right(fl.Name, 4) = ".pdf" Then
                    'MsgBox fl.Name
                    fl.Delete
                End If
                
            Next fl
            
            For Each SubFldr In TopFolder.SubFolders
                DeletePDFs SubFldr.Path
            Next SubFldr
        Else
            MsgBox "No Directory like '" & TopDirectory & "'"
        End If
    End Sub
    
    Sub test()
        DeletePDFs "H:\Test Top Folder"
    End Sub
    But PLEASE test it on something unimportant until you are satisfied it does what you want OK. This cannot be Undone !!

    There is probably a better way but ...??

    HTH


    MTB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •