0

さまざまなサブフォルダーにあるファイルのリストから特定の文字列をファイルに出力できるスクリプトを作成しようとしています。

私のスクリプトは機能しますが、1 つのディレクトリに対してのみ機能します。サブフォルダーで動作させるには助けが必要です

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
4

2 に答える 2

0

ブロック全体を新しいサブルーチンにカプセル化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
于 2013-06-03T21:12:54.750 に答える