2016-03-15 46 views
0

試圖循環訪問我的error.log文件並匹配另一個Map.log(文本)文件中的字符串。我認爲當腳本與error.log文件中的第一行相對於其他Map.log文件中的每一行匹配時,腳本結束時我的循環不正確。這樣做之後,應該移動到下一行的error.log和反對一切Map.log比較等從另一個文本文件中查找字符串

我之所以需要這麼做是error.log文件不包含足夠的信息,以查明來源該問題,但它確實包含日期和時間。我已經手動將它們與Map.log文件中的信息進行匹配以查看具體原因,但Map.log文件中可能有成千上萬行。這可以解決這個問題。基本上,我想匹配map.log中的error.log中的日期/時間,並拉出相應的「FcId:#」。理想的結果是:

3/14/2016 1:20:35 PM: FcId: 98766 
3/14/2016 1:20:39 PM: FcId: 46253
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") 

'============================== 
'This section pulls all of the date and time strings in the error.log file 
'for matching in the maps.log file (everything before the first comma). 
Do While Not ERROR_logfilename.AtEndOfStream 
    arrStr = Split(ERROR_logfilename.ReadLine, ",") 
    strError = arrStr(0) 
    '============================== 
    Do While Not MAP_logfilename.AtEndOfStream 
    strLogItem = MAP_logfilename.ReadLine 

    If InStr(strLogItem, strError) Then 
     WScript.Echo strLogItem 
    End If 
    Loop 
Loop 

ERROR_logfilename.Close 
MAP_logfilename.Close 

error.log文件包含像行:

3/14/2016 1:20:35 PM,Warning in List. 
3/14/2016 1:20:39 PM,Warning in List.

map.log文件中包含這樣的線路:

3/14/2016 1:20:34 PM,key_id: 1 FcId: 987766 ac_id: 987763 prim_1_eftv_dttm: 8/11/2014 prim_2_eftv_dttm: 8/11/2014 
3/14/2016 1:20:34 PM,key_id: 1 FcId: 987766 svr_id: 2872158 br_FcId: 987764 eftv_dttm: 8/11/2014 term_dttm: 3/14/2016 top_md_qty: 1470 btm_md_qty: 1551 
3/14/2016 1:20:34 PM,Record count: 2 
3/14/2016 1:20:34 PM,List.calcLength() Calculated : FcId: 987766 currentDate: 8/11/2014 Length: 81 with 1 webo(s). 
3/14/2016 1:20:35 PM,RESULT for FcId: 987766 GnId: 2585 lbrFcId:: 987764 SeqId: 1 Length: 81 Top: 1470 Btm: 1551 on Date: 8/11/2014 
3/14/2016 1:20:35 PM,Date Range for completion: 987766 : 8/11/2014 12:00:00 AM - 3/14/2016 12:00:00 AM 
3/14/2016 1:20:35 PM,Processed record: 11 Id: 987766 
3/14/2016 1:20:35 PM,End: 
3/14/2016 1:20:35 PM,Begin: 
3/14/2016 1:20:36 PM,key_id: 1 FcId: 29910 ac_id: 29908 prim_1_eftv_dttm: 12/1/1998 prim_2_eftv_dttm: 12/1/1998 
3/14/2016 1:20:36 PM,key_id: 2 FcId: 29910 ac_id: 29908 prim_1_eftv_dttm: 10/1/2000 prim_2_eftv_dttm: 10/1/2000 
3/14/2016 1:20:36 PM,key_id: 1 FcId: 29910 svr_id: 350499 br_FcId: 29909 eftv_dttm: 12/1/1998 term_dttm: 10/1/2000 top_md_qty: 1270 btm_md_qty: 1350 
3/14/2016 1:20:36 PM,key_id: 2 FcId: 29910 svr_id: 350500 br_FcId: 29909 eftv_dttm: 12/1/1998 term_dttm: 10/1/2000 top_md_qty: 1390 btm_md_qty: 1560 
3/14/2016 1:20:36 PM,key_id: 3 FcId: 29910 svr_id: 350501 br_FcId: 29909 eftv_dttm: 12/1/1998 term_dttm: 3/14/2016 top_md_qty: 1620 btm_md_qty: 1800 
3/14/2016 1:20:36 PM,key_id: 4 FcId: 29910 svr_id: 350502 br_FcId: 29909 eftv_dttm: 12/1/1998 term_dttm: 3/14/2016 top_md_qty: 1840 btm_md_qty: 2020 
3/14/2016 1:20:36 PM,Record count: 2 
3/14/2016 1:20:36 PM,List.calcLength() Calculated : FcId: 29910 currentDate: 12/1/1998 Length: 750 with 1 webo(s). 
3/14/2016 1:20:36 PM,RESULT for FcId: 29910 GnId: 3997 lbrFcId:: 29909 SeqId: 1 Length: 82.51 Top: 1270 Btm: 1352.51 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2584 lbrFcId:: 29909 SeqId: 2 Length: 106.27 Top: 1352.51 Btm: 1458.78 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3998 lbrFcId:: 29909 SeqId: 3 Length: 122.22 Top: 1458.78 Btm: 1581 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3999 lbrFcId:: 29909 SeqId: 4 Length: 48.43 Top: 1581 Btm: 1629.43 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2588 lbrFcId:: 29909 SeqId: 5 Length: 76.06 Top: 1629.43 Btm: 1705.49 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2592 lbrFcId:: 29909 SeqId: 6 Length: 34.81 Top: 1705.49 Btm: 1740.3 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2594 lbrFcId:: 29909 SeqId: 7 Length: 16.43 Top: 1740.3 Btm: 1756.73 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2595 lbrFcId:: 29909 SeqId: 8 Length: 25.27 Top: 1756.73 Btm: 1782 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2597 lbrFcId:: 29909 SeqId: 9 Length: 43.5 Top: 1782 Btm: 1825.5 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2598 lbrFcId:: 29909 SeqId: 10 Length: 19.97 Top: 1825.5 Btm: 1845.47 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2599 lbrFcId:: 29909 SeqId: 11 Length: 19.53 Top: 1845.47 Btm: 1865 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2600 lbrFcId:: 29909 SeqId: 12 Length: 8.67 Top: 1865 Btm: 1873.67 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 4007 lbrFcId:: 29909 SeqId: 13 Length: 43.33 Top: 1873.67 Btm: 1917 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2603 lbrFcId:: 29909 SeqId: 14 Length: 45.24 Top: 1917 Btm: 1962.24 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2605 lbrFcId:: 29909 SeqId: 15 Length: 19.76 Top: 1962.24 Btm: 1982 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3989 lbrFcId:: 29909 SeqId: 16 Length: 14.98 Top: 1982 Btm: 1996.98 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2607 lbrFcId:: 29909 SeqId: 17 Length: 15.82 Top: 1996.98 Btm: 2012.8 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3990 lbrFcId:: 29909 SeqId: 18 Length: 7.2 Top: 2012.8 Btm: 2020 on Date: 12/1/1998 
3/14/2016 1:20:37 PM,Date Range for completion: 29910 : 12/1/1998 12:00:00 AM - 10/1/2000 12:00:00 AM 
3/14/2016 1:20:37 PM,Record count: 2 
3/14/2016 1:20:37 PM,List.calcLength() Calculated : FcId: 29910 currentDate: 10/1/2000 Length: 400 with 1 webo(s). 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 3999 lbrFcId:: 29909 SeqId: 1 Length: 9.43 Top: 1620 Btm: 1629.43 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2588 lbrFcId:: 29909 SeqId: 2 Length: 76.06 Top: 1629.43 Btm: 1705.49 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2592 lbrFcId:: 29909 SeqId: 3 Length: 34.81 Top: 1705.49 Btm: 1740.3 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2594 lbrFcId:: 29909 SeqId: 4 Length: 16.43 Top: 1740.3 Btm: 1756.73 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2595 lbrFcId:: 29909 SeqId: 5 Length: 25.27 Top: 1756.73 Btm: 1782 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2597 lbrFcId:: 29909 SeqId: 6 Length: 43.5 Top: 1782 Btm: 1825.5 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2598 lbrFcId:: 29909 SeqId: 7 Length: 19.97 Top: 1825.5 Btm: 1845.47 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2599 lbrFcId:: 29909 SeqId: 8 Length: 19.53 Top: 1845.47 Btm: 1865 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 2600 lbrFcId:: 29909 SeqId: 9 Length: 8.67 Top: 1865 Btm: 1873.67 on Date: 10/1/2000 
3/14/2016 1:20:37 PM,RESULT for FcId: 29910 GnId: 4007 lbrFcId:: 29909 SeqId: 10 Length: 43.33 Top: 1873.67 Btm: 1917 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2603 lbrFcId:: 29909 SeqId: 11 Length: 45.24 Top: 1917 Btm: 1962.24 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2605 lbrFcId:: 29909 SeqId: 12 Length: 19.76 Top: 1962.24 Btm: 1982 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 3989 lbrFcId:: 29909 SeqId: 13 Length: 14.98 Top: 1982 Btm: 1996.98 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 2607 lbrFcId:: 29909 SeqId: 14 Length: 15.82 Top: 1996.98 Btm: 2012.8 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,RESULT for FcId: 29910 GnId: 3990 lbrFcId:: 29909 SeqId: 15 Length: 7.2 Top: 2012.8 Btm: 2020 on Date: 10/1/2000 
3/14/2016 1:20:38 PM,Date Range for completion: 29910 : 10/1/2000 12:00:00 AM - 3/14/2016 12:00:00 AM 
3/14/2016 1:20:38 PM,Processed record: 12 Id: 29910 
3/14/2016 1:20:38 PM,End: 
3/14/2016 1:20:38 PM,Begin: 
3/14/2016 1:20:38 PM,key_id: 1 FcId: 987765 ac_id: 987763 prim_1_eftv_dttm: 8/11/2014 prim_2_eftv_dttm: 8/11/2014 
3/14/2016 1:20:38 PM,key_id: 1 FcId: 987765 svr_id: 2872159 br_FcId: 987764 eftv_dttm: 8/11/2014 term_dttm: 3/14/2016 top_md_qty: 1750 btm_md_qty: 1791 
3/14/2016 1:20:38 PM,Record count: 2 
3/14/2016 1:20:38 PM,List.calcLength() Calculated : FcId: 987765 currentDate: 8/11/2014 Length: 41 with 1 webo(s). 
3/14/2016 1:20:39 PM,RESULT for FcId: 987765 GnId: 2585 lbrFcId:: 987764 SeqId: 1 Length: 41 Top: 1750 Btm: 1791 on Date: 8/11/2014 
3/14/2016 1:20:39 PM,Date Range for completion: 987765 : 8/11/2014 12:00:00 AM - 3/14/2016 12:00:00 AM 
3/14/2016 1:20:39 PM,Processed record: 13 Id: 987765 
3/14/2016 1:20:39 PM,End: 
3/14/2016 1:20:39 PM,Begin: 
3/14/2016 1:20:39 PM,key_id: 1 FcId: 46253 ac_id: 46251 prim_1_eftv_dttm: 12/1/1991 prim_2_eftv_dttm: 12/1/1991 
3/14/2016 1:20:39 PM,key_id: 2 FcId: 46253 ac_id: 46251 prim_1_eftv_dttm: 6/1/1998 prim_2_eftv_dttm: 6/1/1998 
3/14/2016 1:20:39 PM,key_id: 1 FcId: 46253 svr_id: 133009 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 890 btm_md_qty: 911 
3/14/2016 1:20:39 PM,key_id: 2 FcId: 46253 svr_id: 133010 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 911 btm_md_qty: 968 
3/14/2016 1:20:39 PM,key_id: 3 FcId: 46253 svr_id: 133011 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 968 btm_md_qty: 1036 
3/14/2016 1:20:39 PM,key_id: 4 FcId: 46253 svr_id: 133013 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1036 btm_md_qty: 1070 
3/14/2016 1:20:39 PM,key_id: 5 FcId: 46253 svr_id: 133015 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1090 btm_md_qty: 1120 
3/14/2016 1:20:39 PM,key_id: 6 FcId: 46253 svr_id: 133017 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1120 btm_md_qty: 1172 
3/14/2016 1:20:39 PM,key_id: 7 FcId: 46253 svr_id: 133018 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1172 btm_md_qty: 1220 
3/14/2016 1:20:39 PM,key_id: 8 FcId: 46253 svr_id: 133021 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1240 btm_md_qty: 1320 
3/14/2016 1:20:39 PM,key_id: 9 FcId: 46253 svr_id: 133024 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1320 btm_md_qty: 1380 
3/14/2016 1:20:39 PM,key_id: 10 FcId: 46253 svr_id: 133026 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1400 btm_md_qty: 1430 
3/14/2016 1:20:39 PM,key_id: 11 FcId: 46253 svr_id: 133027 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1430 btm_md_qty: 1450 
3/14/2016 1:20:39 PM,key_id: 12 FcId: 46253 svr_id: 133030 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1450 btm_md_qty: 1510 
3/14/2016 1:20:39 PM,key_id: 13 FcId: 46253 svr_id: 133031 br_FcId: 46252 eftv_dttm: 10/16/1991 term_dttm: 3/14/2016 top_md_qty: 1510 btm_md_qty: 1540 
3/14/2016 1:20:39 PM,Record count: 2 
3/14/2016 1:20:39 PM,List.calcLength() Calculated : FcId: 46253 currentDate: 12/1/1991 Length: 650 with 1 webo(s). 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2584 lbrFcId:: 46252 SeqId: 1 Length: 40.86 Top: 890 Btm: 930.86 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3998 lbrFcId:: 46252 SeqId: 2 Length: 99.04 Top: 930.86 Btm: 1029.9 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3999 lbrFcId:: 46252 SeqId: 3 Length: 55.1 Top: 1029.9 Btm: 1085 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2588 lbrFcId:: 46252 SeqId: 4 Length: 81.13 Top: 1085 Btm: 1166.13 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2592 lbrFcId:: 46252 SeqId: 5 Length: 59.17 Top: 1166.13 Btm: 1225.3 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2594 lbrFcId:: 46252 SeqId: 6 Length: 62.35 Top: 1225.3 Btm: 1287.65 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2597 lbrFcId:: 46252 SeqId: 7 Length: 141.25 Top: 1287.65 Btm: 1428.9 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 4007 lbrFcId:: 46252 SeqId: 8 Length: 15.7 Top: 1428.9 Btm: 1444.6 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2603 lbrFcId:: 46252 SeqId: 9 Length: 54.36 Top: 1444.6 Btm: 1498.96 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 2605 lbrFcId:: 46252 SeqId: 10 Length: 29.7 Top: 1498.96 Btm: 1528.66 on Date: 12/1/1991 
3/14/2016 1:20:40 PM,RESULT for FcId: 46253 GnId: 3989 lbrFcId:: 46252 SeqId: 11 Length: 11.34 Top: 1528.66 Btm: 1540 on Date: 12/1/1991
+0

這是因爲您的地圖文件已經在流的末尾。您需要重置地圖文件以再次閱讀它。 – Sorceri

+0

這很有道理。當你說「重置」時,我該如何重置循環?聽起來像「雖然不MAP_logfilename.AtEndOfStream」是問題。不太確定如何使它從頂部重新啓動。我會調查...也許在error.log文件中計算行數,並且多次執行for/next ....? – user3108489

+0

我會將整個文件讀入一個字符串,將它拆分爲換行符並將其放入數組中。然後你可以循環訪問數組來檢查。 – Sorceri

回答

1

您可能需要調整這樣它就能找到正確的換行符。

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

謝謝。做了一些調整,以適應我的需求,但這個例子讓我知道我需要什麼。謝謝。我會發布我的最終腳本。 – user3108489

1

這是我結束了感謝Sorceri的幫助下最終腳本。

'=================================== 
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 

'================================== 
相關問題