Vbscript list all PDF files in folder and subfolders

前端 未结 7 1635
甜味超标
甜味超标 2020-12-03 14:18

Well here is my code but I just can not filter the listing using the objFile.Extension i am sure it is some thing silly

Set objFSO = CreateObject(\"Scripting         


        
相关标签:
7条回答
  • 2020-12-03 14:53

    You'll want to use the GetExtensionName method on the FileSystemObject object.

    Set x = CreateObject("scripting.filesystemobject")
    WScript.Echo x.GetExtensionName("foo.pdf")
    

    In your example, try using this

    For Each objFile in colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
            Wscript.Echo objFile.Name
        End If
    Next
    
    0 讨论(0)
  • 2020-12-03 14:53

    There's a well documented answer to your question at this url:

    http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/18/how-can-i-list-the-files-in-a-folder-and-all-its-subfolders.aspx

    The answer shown at that URL is kind of complicated and uses WMI (Windows Management Instrumentation) to iterate through files and folders. But if you do a lot of Windows administration, it's worth the effort to learn WMI.

    I'm posting this now in case you need something right now; but I think I used to use a filesystemobject based approach, and I'll look for some example, and I'll post it later if I find it.

    I hope this is helpful.

    0 讨论(0)
  • 2020-12-03 14:55

    (For those who stumble upon this from your search engine of choice)

    This just recursively traces down the folder, so you don't need to duplicate your code twice. Also the OPs logic is needlessly complex.

    Wscript.Echo "begin."
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objSuperFolder = objFSO.GetFolder(WScript.Arguments(0))
    Call ShowSubfolders (objSuperFolder)
    
    Wscript.Echo "end."
    
    WScript.Quit 0
    
    Sub ShowSubFolders(fFolder)
        Set objFolder = objFSO.GetFolder(fFolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
                Wscript.Echo objFile.Name
            End If
        Next
    
        For Each Subfolder in fFolder.SubFolders
            ShowSubFolders(Subfolder)
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-03 14:55

    Check this code :

    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    
    objStartFolder = "C:\Folder1\"
    
    Set objFolder = objFSO.GetFolder(objStartFolder)
    
    Set colFiles = objFolder.Files
    
    For Each objFile in colFiles 
        strFileName = objFile.Name
    
        If objFSO.GetExtensionName(strFileName) = "pdf" Then 
            Wscript.Echo objFile.Name 
        End If
    Next
    
    ShowSubfolders objFSO.GetFolder(objStartFolder)
    
    Sub ShowSubFolders(Folder)
    
        For Each Subfolder in Folder.SubFolders 
            Set objFolder = objFSO.GetFolder(Subfolder.Path) 
            Set colFiles = objFolder.Files 
            for each Files in colFiles 
                if LCase(InStr(1,Files, ".pdf")) > 1 then Wscript.Echo Files 
            next
            ShowSubFolders Subfolder 
        Next 
    End Sub
    
    0 讨论(0)
  • 2020-12-03 15:01

    May not help OP, but hopefully others may find this helpful:

    run

    %ComSpec% /c cd/d StartPath & dir/s/b *.pdf
    

    using shell object

    StdOut will contain all PDF files

    0 讨论(0)
  • 2020-12-03 15:03
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objStartFolder = "C:\Users\NOLA BOOTHE\My Documents\operating system"
    
    Set objFolder = objFSO.GetFolder(objStartFolder)
    
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
        Wscript.Echo objFile.Name
    Next
    
    0 讨论(0)
提交回复
热议问题