2013-06-03 198 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 
相關問題