5

我正在尝试编写一个脚本,它将搜索 C:\ 及其所有子文件夹以查找特定扩展名,并将所有主题保存到 CSV 文件中。我试过这个但无济于事:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.GetExtensionName("*.txt")

Set colFiles = objFolder.Files

For Each objFile in colFiles
If objFile.Extension = "pfx" 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

Set WScript = CreateObject("WScript.Shell")

End Sub

我不认为我在这里走的是正确的道路。我至少不精通 vb 脚本,它恰好是我唯一被允许使用的东西。

4

3 回答 3

19

干得好:

Option Explicit 'force all variables to be declared

Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\Output.txt", ForWriting, True)

Recurse objFSO.GetFolder("C:\")
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "pfx" Then
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub
于 2013-09-20T16:03:41.507 回答
2

我知道这有点晚了,但这是获得该列表的艰难方法。最简单的是起诉命令行:

首先确定您想要目录的文件,如下所示:

set FileList="C:\path\path\file.csv"

其次确定这样的起始目录:

set Start="C:\path\path"

然后像这样填充它:

for /F "usebackq" %i in (`dir /s/b "%Start%\*.pfx"`) do echo %~pi>>%filelist%

以交互方式完成和完成。

PS - 帖子去掉了反引号。这通常是波浪线下的字符,它们应该包含完整的目录命令。假设替换 ! 对于反引号,目录命令应为!dir/s/b "%Start%\*.pfx"!

于 2016-01-22T20:12:49.273 回答
1

稍后但更简单(和快速:0)):

打开 CMD,CD 到有问题的目录

DIR /S /AD /BC:\*.something >> D:\ListOf.Files

于 2017-06-08T04:56:29.520 回答