Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vbscript list all PDF files in folder and subfolders

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.FileSystemObject")
objStartFolder = "C:\dev"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path

Set colFiles = objFolder.Files

For Each objFile in colFiles
If objFile.Extension = "PDF" Then
    Wscript.Echo objFile.Name
    End If
Next
Wscript.Echo

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            Wscript.Echo objFile.Name
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next
End Sub

On run it comes back with the error

(11, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'objFile.Extension'

like image 438
John Avatar asked Nov 16 '10 23:11

John


1 Answers

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
like image 103
jveazey Avatar answered Sep 19 '22 20:09

jveazey