【问题标题】:Read files in subfolders读取子文件夹中的文件
【发布时间】:2013-05-30 02:35:26
【问题描述】:

我正在尝试制作一个脚本,我们可以从不同子文件夹中的文件列表中将特定字符串输出到文件中。

我的脚本有效,但仅适用于一个目录。我需要一些帮助才能使其与子文件夹一起使用

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("D:\vbs\logs\") ' here i have loads of subfolders with *.txt 
Set outfile = objFSO.CreateTextFile("D:\vbs\ok\test.txt") ' my output file

for each file in folder.Files
    Set testfile = objFSO.OpenTextFile(file.path, ForReading)

    Do While Not testfile.AtEndOfStream

        If instr (testfile.readline, "central") then ' i output every lines where there is the word "central" 
            outfile.writeline testfile.readline
        End If

        if instr (testfile.readline, "version") then  ' i use this to parse my output file to get a indication between every files read

            num = testfile.readline
            mag = Split(num)
        elseif testfile.AtEndOfStream = true then
            outfile.writeline "Shop " & mag(4)
        end if
    Loop
    testfile.close
next
outfile.close

【问题讨论】:

  • 此任务的工具 - 例如。 findstr 或 grep。使用它们或说明为什么不使用。
  • 我真的很想使用VBS,因为我花了很多时间来那里。现在,如果我能在子文件夹中使用它,我会为我感到自豪(我是初学者)
  • 请参阅stackoverflow.com/a/9454363/603855,了解在递归遍历中使用文件的分步介绍;请参阅stackoverflow.com/a/16895790/603855 了解获取文件夹树中所有文件列表的想法,然后可以按顺序处理。

标签: vbscript text-files subdirectory


【解决方案1】:

我会将您的整个For...Each 块封装到一个新的子例程中,然后添加一个新的For...Each 块以捕获父文件夹中的所有subFolders。我已将该功能添加到您的脚本中,请参见下文。

Const ForReading = 1
Const Start_Folder = "D:\vbs\logs\" ' here i have loads of subfolders with *.txt 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outfile = objFSO.CreateTextFile("D:\vbs\ok\test.txt") ' my output file


'Call the Search subroutine to start the recursive search.
Search objFSO.GetFolder(Start_Folder)

'Close the outfile after all folders have been searched
outfile.Close

Sub Search(sDir)

    for each file in sDir.Files
        Set testfile = objFSO.OpenTextFile(file.path, ForReading)

        Do While Not testfile.AtEndOfStream

            If instr (testfile.readline, "central") then ' i output every lines where there is the word "central" 
                outfile.writeline testfile.readline
            End If

            if instr (testfile.readline, "version") then  ' i use this to parse my output file to get a indication between every files read

                num = testfile.readline
                mag = Split(num)
            elseif testfile.AtEndOfStream = true then
                outfile.writeline "Shop " & mag(4)
            end if
        Loop
        testfile.close
    next

    'Find EACH SUBFOLDER.
    For Each subFolder In sDir.SubFolders

        'Call the Search subroutine to start the recursive search on EACH SUBFOLDER.
        Search objFSO.GetFolder(subFolder.Path)
    Next

End Sub

【讨论】:

    【解决方案2】:

    有关文件夹递归示例,请参阅this answer 的类似问题。

    关于您现有代码的一点说明:每次调用 ReadLine 方法都会从文件中读取下一行,因此如下所示:

    If instr (testfile.readline, "central") then
      outfile.writeline testfile.readline
    End If
    

    输出包含单词“central”的行(如您的 cmets 所说),而是输出该行之后的行。

    如果要输出包含正在检查的单词的行,则必须将读取的行存储在变量中并继续使用该变量:

    line = testfile.ReadLine
    If InStr(line, "central") Then
      outfile.WriteLine line
    End If
    

    【讨论】:

    • @ekkehard.horner 感谢您纠正错字。我想我现在应该去睡觉了……;-\
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多