本文介绍了Vbscript 搜索所有带有扩展名的文件并将它们保存到 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本将搜索 C:\ 及其所有子文件夹以查找特定扩展名,并将所有主题保存到 CSV 文件中.我试过了,但没有用:

I am trying to write a script that will search say C:\ and all of its sub folders for a specific extension and save all of theme to a CSV file. I have tried this but to no avail:

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 脚本,它恰好是我唯一可以使用的东西.

I don't think I am going down the right path here. I am not proficient in the least in vb script it just happens to be the only thing I am allowed to use.

推荐答案

给你:

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

这篇关于Vbscript 搜索所有带有扩展名的文件并将它们保存到 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:49
查看更多