Vbscript列出文件夹和子文件夹中的所有PDF文件
好吧,这是我的代码,但我无法使用 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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要使用 GetExtensionName FileSystemObject 对象上的方法。
在你的例子中,尝试使用这个
You'll want to use the GetExtensionName method on the FileSystemObject object.
In your example, try using this
(对于那些从您选择的搜索引擎中偶然发现此内容的人)
这只是递归地跟踪文件夹,因此您不需要重复代码两次。 OP 逻辑也不必要地复杂。
(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.
文件扩展名可能区分大小写...但代码有效。
The file extension may be case sentive...but the code works.
可能对OP没有帮助,但希望其他人可能会发现这有帮助:
StdOut运行
使用shell对象
将包含所有PDF文件
May not help OP, but hopefully others may find this helpful:
run
using shell object
StdOut will contain all PDF files
此网址对您的问题有详细记录的答案:
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 是值得的。
我现在发布此内容,以防您现在需要某些东西;但我想我曾经使用基于文件系统对象的方法,我会寻找一些例子,如果找到的话我会稍后发布。
我希望这有帮助。
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.
检查此代码:
Check this code :