2013-06-03 145 views
0

我想制作一个脚本,我们可以从不同的子文件夹中的文件列表中输出特定的字符串ino文件。阅读子文件夹中的文件

我的脚本可以在一个目录上运行,但是只能运行一个目录。我需要一些帮助,使它与子

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 
+0

此工具可用于此任务 - 例如, findstr或grep。使用它们或说明为什么不。 –

+0

我真的很想使用VBS,因为我花了很多时间来到那里。现在,如果只有我可以在子文件夹中使用它,我会为我感到骄傲(我是初学者) – user2449297

+0

请参阅http://stackoverflow.com/a/9454363/603855一步一步介绍如何在递归遍历中使用文件;请参阅http://stackoverflow.com/a/16895790/603855以获取文件夹树中所有文件的列表,然后按顺序处理该列表。 –

回答

1

this answer到一个文件夹递归例子类似的问题。关于现有的代码

一个的话,虽然:在ReadLine方法的每个调用从文件中读取下一行,所以像这样:

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

输出包含字线“中央“(如你的意见所述),但行那一行。

如果你想输出包含你正在检查的字行,你必须读取的行存储在一个变量,并继续与该变量:

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

@ ekkehard.horner感谢您修复错字。我想我现在应该去睡觉......; - \ –

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 
相关问题