2010-11-16 177 views
14

那麼這裏是我的代碼,但我只是不能使用objFile.Extension篩選上市我相信這是一些無聊的事情VBSCRIPT列表文件夾中的所有PDF文件和子文件夾

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 runtime error: Object doesn't support this property or method: 'objFile.Extension'

回答

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 
+0

謝謝約翰,做了它 但我有問題試圖過濾子文件夾我沒有太多的運氣 – John 2010-11-18 06:21:56

+1

你能更具體嗎?你是否得到錯誤或其他什麼? – jveazey 2010-11-19 06:51:28

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

在該網址中顯示的答案是一種並且使用WMI(Windows Management Instrumentation)來遍歷文件和文件夾。但是如果你做了很多Windows管理,那麼學習WMI是值得的。

我現在發佈此信息以防萬一您需要某些東西;但我認爲我曾經使用基於文件系統對象的方法,我會尋找一些例子,如果我找到它,我會稍後發佈。

我希望這是有幫助的。

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 
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

文件擴展名可能是區分sentive ...但代碼工作。

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 
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 
1

可能無法幫助OP,但希望其他人可能會有所幫助:使用shell對象

運行

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

StdOut wi將包含所有的PDF文件

+0

如何找出誰降低了這一點?想知道爲什麼。 – tnana 2016-08-28 00:10:30

相關問題