2013-05-28 89 views
0

我有有成千上萬的每行的幾個文本文件與此是一個典型的線的一個實例的後續文本:返回行號或文本文件字符串搜索

PCI\VEN_10EC&DEV_8168&REV_09 Realtek\5x64\FORCED\PCIe_5.810.1218.2012\ Netrtle.inf Realtek 1 12/18/2012,5.810.1218.2012 Realtek PCIe GBE Family Controller 

腳本我的工作對確實爲文本,它的第一段的字符串搜索:

PCI\VEN_10EC&DEV_8168&REV_09 

我的腳本縮小了哪些文件具有此字符串,但我真正需要的是它,然後返回在同一條線上的下一個字符串:

Realtek\5x64\FORCED\PCIe_5.810.1218.2012\ 

一旦我有了這個字符串,我就可以繼續使用只從7zip中提取Realtek文件夾的腳本的其餘部分。

我已經看到這與Stack的其他語言完成,但我找不到任何VBS。如果我知道如何更好地說明任務,我可能會找到答案。我真的很感激一些建議抓住第二個字符串。

對於背景,這是我正在處理的腳本。它看起來在所有的文本文件在C:\腳本\通過WMI查詢代碼爲28返回CompatibleID設備驅動程序的字符串(未安裝驅動程序):

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objShell = CreateObject("Wscript.Shell") 
Set objNet = CreateObject("WScript.Network") 
Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery _ 
    ("Select * from Win32_PnPEntity " _ 
     & "WHERE ConfigManagerErrorCode = 28") 
For Each objItem in colItems 
Dim arrCompatibleIDs 
aarCompatibleIDs = objItem.CompatibleID 
for each objComp in aarCompatibleIDs 
Dim FirstID 
FirstID = objComp 
Exit For 
Next 
Next 

strSearchFor = firstID 
objStartFolder = "C:\scripts" 

Set objFolder = objFSO.GetFolder(objStartFolder) 

Set colFiles = objFolder.Files 
For Each objFile in colFiles 
    'Wscript.Echo objFile.Name 
    strFile = "C:\scripts\" & objFile.Name 
    set objFile = objFSO.getFile(strFile) 
    if objFile.size > 0 then 
     If InStr(objFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then 
      msgbox(objfile.name) 
     Else 
      WScript.Sleep (100) 
     End If 
    End If 
Next 

回答

0

如果您需要尋找一個固定的針和乾草堆中的變量線程,可以使用一些InStr()或RegExp。爲了讓你開始:

Dim sHaystack : sHaystack = Join(Array(_ 
     "hay hay" _ 
    , "fixed_needle variable_thread hay" _ 
    , "hay hay" _ 
), vbCrLf) 
    Dim sNeedle : sNeedle = "fixed_needle" & " " 
    Dim nPosN : nPosN = Instr(sHaystack, sNeedle) 
    If 0 < nPosN Then 
    nPosN = nPosN + Len(sNeedle) 
    Dim nPosT : nPosT = Instr(nPosN, sHaystack, " ") 
    If 0 < nPosN Then 
     WScript.Echo "Instr()", qq(Mid(sHaystack, nPosN, nPosT - nPosN)) 
    Else 
     WScript.Echo "no thread" 
    End If 
    Else 
    WScript.Echo "no needle" 
    End If 
    Dim reNT : Set reNT = New RegExp 
    reNT.Pattern = sNeedle & "(\S+) " 
    Dim oMTS : Set oMTS = reNT.Execute(sHayStack) 
    If 1 = oMTS.Count Then 
    WScript.Echo "RegExp ", qq(oMTS(0).SubMatches(0)) 
    Else 
    WScript.Echo "no match" 
    End If 

輸出:

Instr() "variable_thread" 
RegExp "variable_thread" 

如果你改變了乾草堆到

Dim sHaystack : sHaystack = Join(Array(_ 
     "hay hay" _ 
    , "fixed_needle no_variable_thread_hay" _ 
    , "hay hay" _ 
), vbCrLf) 

輸出:

Instr() "no_variable_thread_hay 
hay" 
no match 

你看到有更多的工作需要製作Instr()appr oach防彈。

0

由於輸入文件似乎是製表符分隔,你可以做這樣的事情:

Set wmi = GetObject("winmgmts://./root/cimv2") 
qry = "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 28" 
For Each entity In wmi.ExecQuery(qry) 
    For Each cid In entity.CompatibleID 
    firstID = cid 
    Exit For 
    Next 
Next 

Set fso = CreateObject("Scripting.FileSystemObject") 
For Each f In objFSO.GetFolder(objStartFolder).Files 
    If f.Size > 0 Then 
    For line In Split(f.OpenAsTextStream.ReadAll, vbNewLine) 
     arr = Split(line, vbTab) 
     If arr(0) = firstID Then MsgBox arr(1) 
    Next 
    End If 
Next 

在更廣泛的筆記,你不應該做這樣的東西:

Set colFiles = objFolder.Files 
For Each objFile in colFiles 
    strFile = "C:\scripts\" & objFile.Name 
    set objFile = objFSO.getFile(strFile) 
    if objFile.size > 0 then 
    If InStr(objFSO.OpenTextFile(strFile).ReadAll, strSearchFor) > 0 Then 
... 

Files集合已包含File對象,因此根據對象的屬性構建路徑名是完全沒有意義的(它包含Path屬性,給你完整的路徑)只能獲得你已經擁有完全相同的對象。另外,文件對象有一個方法OpenAsTextStream,所以你可以直接打開它們作爲文本文件,而不需要像objFSO.OpenTextFile(f.Path)那樣繞道。

相關問題