Parsing 从一个文本文件在另一个文本文件中查找字符串
尝试循环查看我的Parsing 从一个文本文件在另一个文本文件中查找字符串,parsing,vbscript,split,Parsing,Vbscript,Split,尝试循环查看我的error.log文件,并匹配另一个Map.log(文本)文件中的字符串。我认为我循环错误,因为脚本在将error.log文件中的第一行与另一Map.log文件中的每一行匹配后结束。这样做之后,它应该移动到error.log中的下一行,并与Map.log中的所有内容进行比较,等等 我需要这样做的原因是error.log文件没有包含足够的信息来确定问题的根源,但它确实包含日期和时间。我一直在手动将它们与Map.log文件中的信息进行匹配,以查看具体原因,但是Map.log文件中可能
error.log
文件,并匹配另一个Map.log
(文本)文件中的字符串。我认为我循环错误,因为脚本在将error.log
文件中的第一行与另一Map.log
文件中的每一行匹配后结束。这样做之后,它应该移动到error.log
中的下一行,并与Map.log中的所有内容进行比较,等等
我需要这样做的原因是error.log
文件没有包含足够的信息来确定问题的根源,但它确实包含日期和时间。我一直在手动将它们与Map.log
文件中的信息进行匹配,以查看具体原因,但是Map.log
文件中可能有数千行。这将弥补这一缺陷。基本上,我想匹配error.log中的日期/时间,然后在map.log中拉出相应的“FcId:#”。理想的结果是:
3/14/2016 1:20:35 PM: FcId: 98766
3/14/2016 1:20:39 PM: FcId: 46253
error.log
文件包含以下行:
3/14/2016 1:20:35 PM,Warning in List.
3/14/2016 1:20:39 PM,Warning in List.
您可能需要对其进行调整,使其拾取正确的换行符
'===================================
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")
errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)
ERROR_logfilename.Close 'close the file
MAP_logfilename.Close
For i = 0 To UBound(errorContents) - 1 'loop through error log contents
arrStr = Split(errorContents(i), ",")
strError = arrStr(0)
For j = 0 To UBound(mapContents) - 1 'loop through map log contents
If InStr(mapContents(j), strError) Then
wscript.echo strError & " " & strMap
End If
Next j
Next i
您可能需要对其进行调整,使其拾取正确的换行符
'===================================
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")
errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)
ERROR_logfilename.Close 'close the file
MAP_logfilename.Close
For i = 0 To UBound(errorContents) - 1 'loop through error log contents
arrStr = Split(errorContents(i), ",")
strError = arrStr(0)
For j = 0 To UBound(mapContents) - 1 'loop through map log contents
If InStr(mapContents(j), strError) Then
wscript.echo strError & " " & strMap
End If
Next j
Next i
多亏了索切里的帮助,这是我最后的剧本
'===================================
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")
strOutputFileName = fso.GetParentFolderName(WScript.ScriptFullName) & "\Error_Clean.log"
Set objFile = fso.OpenTextFile(strOutputFileName, 2 , true) 'Clear old errors
objFile.Write ""
objFile.Close
errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)
ERROR_logfilename.Close 'close the file
MAP_logfilename.Close
For i = 0 To UBound(errorContents) - 1 'loop through error log contents
arrStr = Split(errorContents(i), ",")
strError = arrStr(0)
For j = 0 To UBound(mapContents) - 1 'loop through map log contents
If InStr(mapContents(j), strError) and InStr(mapContents(j), "PM,Processed record: ") Then
arrStr1 = Split(mapContents(j),"CmplId: ")
strMapContents = arrStr1(1)
'wscript.echo strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents
CleanErrors = strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents & vbCrlF
Set objFile = fso.OpenTextFile(strOutputFileName, 8 , true) 'Write new errors
objFile.WriteLine CleanErrors
objFile.Close
End If
Next
Next
'==================================
多亏了索切里的帮助,这是我最后的剧本
'===================================
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ERROR_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Error.log")
Set MAP_logfilename = fso.OpenTextFile(fso.GetParentFolderName(wscript.ScriptFullName) & "\Map.log")
strOutputFileName = fso.GetParentFolderName(WScript.ScriptFullName) & "\Error_Clean.log"
Set objFile = fso.OpenTextFile(strOutputFileName, 2 , true) 'Clear old errors
objFile.Write ""
objFile.Close
errorContents = Split(ERROR_logfilename.ReadAll, vbCrLf) 'get the contents and split on the line feed
mapContents = Split(MAP_logfilename.ReadAll, vbCrLf)
ERROR_logfilename.Close 'close the file
MAP_logfilename.Close
For i = 0 To UBound(errorContents) - 1 'loop through error log contents
arrStr = Split(errorContents(i), ",")
strError = arrStr(0)
For j = 0 To UBound(mapContents) - 1 'loop through map log contents
If InStr(mapContents(j), strError) and InStr(mapContents(j), "PM,Processed record: ") Then
arrStr1 = Split(mapContents(j),"CmplId: ")
strMapContents = arrStr1(1)
'wscript.echo strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents
CleanErrors = strError & vbCrlF & "Cmpl_Fac_ID: " & strMapContents & vbCrlF
Set objFile = fso.OpenTextFile(strOutputFileName, 8 , true) 'Write new errors
objFile.WriteLine CleanErrors
objFile.Close
End If
Next
Next
'==================================
这是因为您的映射文件已经位于流的末尾。您需要重置映射文件以再次读取它。这很有意义。当你说“重置”时,如何重置循环?听起来像是“Do While Not MAP_logfilename.AtEndOfStream”就是问题所在。不太清楚如何使它从顶部重新启动。我会调查。。。可能是计算error.log文件中的行数并多次执行for/next…?我会将整个文件读入一个字符串,在换行符上拆分,然后将其放入数组中。然后你就可以通过数组循环检查了。在阵列上的新手。我将对此进行研究,首先将strLogItem=MAP\u logfilename.ReadLine更改为strLogItem=MAP\u logfilename.ReadAll我猜是这样。尝试使用您的建议。看看我是否正确地实现了它们。这是因为您的映射文件已经位于流的末尾。您需要重置映射文件以再次读取它。这很有意义。当你说“重置”时,如何重置循环?听起来像是“Do While Not MAP_logfilename.AtEndOfStream”就是问题所在。不太清楚如何使它从顶部重新启动。我会调查。。。可能是计算error.log文件中的行数并多次执行for/next…?我会将整个文件读入一个字符串,在换行符上拆分,然后将其放入数组中。然后你就可以通过数组循环检查了。在阵列上的新手。我将对此进行研究,首先将strLogItem=MAP\u logfilename.ReadLine更改为strLogItem=MAP\u logfilename.ReadAll我猜是这样。尝试使用您的建议。看看我是否正确地实现了它们。谢谢。做了一些调整以满足我的需要,但这个例子满足了我的需要。非常感谢。我会发布我的最终剧本。谢谢。做了一些调整以满足我的需要,但这个例子满足了我的需要。非常感谢。我将发布我的最终脚本。