Vbscript列出文件夹和子文件夹中的所有PDF文件

时间:2010-11-16 23:16:25

标签: vbscript directory subdirectory directory-listing

这里是我的代码,但我无法使用objFile.Extension过滤列表我确定这是件事傻事

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

在运行时,它会返回错误

  

(11,1)Microsoft VBScript运行时错误:对象不支持此操作   属性或方法:'objFile.Extension'

7 个答案:

答案 0 :(得分:24)

您需要在FileSystemObject对象上使用GetExtensionName方法。

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

在您的示例中,请尝试使用此

For Each objFile in colFiles
    If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
        Wscript.Echo objFile.Name
    End If
Next

答案 1 :(得分:11)

(对于那些从您选择的搜索引擎中偶然发现此事的人)

这只是递归地追踪文件夹,因此您不需要复制代码两次。 OP的逻辑也是不必要的复杂。

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

答案 2 :(得分:4)

文件扩展名可能区分大小写......但代码可以正常工作。

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
  strFileName = objFile.Name

  If objFSO.GetExtensionName(strFileName) = "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

答案 3 :(得分:2)

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

答案 4 :(得分:2)

可能无法帮助OP,但希望其他人可能会觉得这很有用:

运行

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

使用shell对象

StdOut将包含所有PDF文件

答案 5 :(得分:1)

在这个网址上有一个记录清楚的答案:

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

该URL显示的答案有点复杂,并使用WMI(Windows Management Instrumentation)来遍历文件和文件夹。但是如果你做了很多Windows管理,那么学习WMI是值得的。

我现在发布这个,以防你现在需要一些东西;但我认为我曾经使用过基于文件系统对象的方法,我会找一些例子,如果找到它我会稍后发布。

我希望这有用。

答案 6 :(得分:1)

检查此代码:

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