15

好吧,这是我的代码,但我无法使用 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'

4

7 回答 7

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
于 2010-11-17T02:58:29.030 回答
11

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

这只是递归地跟踪文件夹,所以你不需要重复你的代码两次。此外,OPs 逻辑是不必要的复杂。

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
于 2011-11-20T07:05:05.220 回答
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
于 2013-07-11T22:31:55.550 回答
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
于 2012-02-28T21:18:13.910 回答
2

可能对 OP 没有帮助,但希望其他人可能会发现这有帮助:

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

使用外壳对象

StdOut 将包含所有 PDF 文件

于 2016-08-27T20:31:45.403 回答
1

在这个 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

该 URL 上显示的答案有点复杂,并使用 WMI(Windows 管理规范)来遍历文件和文件夹。但是,如果您从事大量 Windows 管理工作,那么学习 WMI 是值得的。

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

我希望这是有帮助的。

于 2011-06-04T22:23:45.303 回答
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
于 2013-08-21T14:51:17.680 回答