1

I'm trying to make a script that we can output a specific string ino a files from a list of files in different subfolders.

My script works but onl for one directory. I need some help to make it works with subfolders

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
user2449297
  • 11
  • 1
  • 3
  • There tools for this task - e.g. findstr or grep. Use them or state why not. – Ekkehard.Horner Jun 03 '13 at 20:35
  • I really want to use VBS cause i have spend much time to come there. Now if only i can use it in subfolders i would be proud of me ( i am beginner ) – user2449297 Jun 03 '13 at 20:49
  • see http://stackoverflow.com/a/9454363/603855 for a step by step introduction to work with files in recursive traversal; see http://stackoverflow.com/a/16895790/603855 for an idea to get a list of all files in a folder tree that then can be processed sequentially. – Ekkehard.Horner Jun 03 '13 at 21:03

2 Answers2

2

See this answer to a similar question for a folder recursion example.

One remark about your existing code, though: each call of the ReadLine method reads the next line from the file, so something like this:

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

will not output the line containing the word "central" (as your comments say), but the line after that line.

If you want to output the line containing the word you're checking for, you have to store the read line in a variable and continue with that variable:

line = testfile.ReadLine
If InStr(line, "central") Then
  outfile.WriteLine line
End If
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

I would encapsulate your entire For...Each block into a new subroutine and then add a new For...Each block to capture all subFolders in the parent folder. I added that functionality to your script, see below.

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
J P
  • 274
  • 1
  • 2
  • 7