AutoHotkey Community

It is currently May 27th, 2012, 5:59 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: September 24th, 2010, 3:30 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
Hi

I have a file reading loop that doesn't work exactly as expected. I have looked at it for some time and cant seem to work it out.

Anyway what I am trying to do is to read the Windows scheduled task log and report on any failed tasks. These failed tasks are represented in the log with a line that has exit code of (n) where n is a number between 1 & 9.

Once a failed task is found, I want to loop back up thru the log file, capturing all the lines until I hit a line that starts with a " . The " indicates the task name that has failed. Once the line with the " has been found, then the script should continue reading the log.

Below is the code I have

Code:
FileRead, SchedLgU, SchedLgU.txt
; below is the code to read each line
; when a match is found read back through the file to get the previous lines until the line starting with a " is found
Loop, Parse, SchedLgU,`n, `r
   {
   ;msgbox in file read loop `n%A_LoopField%
   
   ; skip blank lines
   If A_LoopField =
      Continue

   ; define and store all read lines as variables
   Row:=A_Index
   Previous:=Row-1
   theline%Row% := A_LoopField

   ; look for string at the end of the line that is (n). and not (0). 0 indicates success
   If (RegExMatch(A_LoopField, "^.*\([^0]\).$"))
      {

      ; define the search string. when this string found at start of line, stop looking
      searchSTR := Chr(34)
      Loop
         {

         ;msgbox % "Row " . Previous . "`nThis is the previous line`n" . theline%Previous%
         If SubStr(theline%Previous%, 1 , 1) = searchSTR
            {
            ;msgbox % "If statement in loop`nRow " . Previous . "`nThis is the previous line`n" . theline%Previous%
            thiserrormessage .= theline%Previous%  . "`n"
            Break
            }
         Else
            {
            ;msgbox % "Else statement in loop`nRow " . Previous . "`nThis is the previous line`n" . theline%Previous%
            thiserrormessage .= theline%Previous% . "`n"
            
            ; subtract 1 for next loop
            Previous--
            }
         }
         ; after getting previous line(s) , get the matched line
         thiserrormessage .= A_LoopField . "`n"
      }
   }
msgbox "START`n%thiserrormessage%FINISH"


Below is the log file.
Code:
"01 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   01 Result: The task completed with an exit code of (1).
"11 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   11 Finished 9/20/2010 8:21:00 AM
   11 More 9/20/2010 8:21:00 AM
   11 MoreMore 9/20/2010 8:21:00 AM
   11 Result: The task completed with an exit code of (1).
"22 TEST123 - Raise Alert for Rejected ACK for WBC.job" (perl)
   22 Finished 9/20/2010 8:22:00 AM
   22 Result: The task completed with an exit code of (2).
"33 TEST123 - VIF Sanity Script Alert.job" (bash.exe)
   33 Finished 9/20/2010 8:23:00 AM
   33 Result: The task completed with an exit code of (3).
"44 TEST123 - Copy Secure Script Bash.job" (bash.exe)
   44 Finished 9/20/2010 8:24:00 AM
   44Result: The task completed with an exit code of (4).
"55 TEST123 - VIF Sanity Script.job" (bash.exe)
   55 Finished 9/20/2010 8:25:00 AM
   55 Result: The task completed with an exit code of (5).
"66 Storage Space Monitor.job" (perl.exe)
   66 Finished 9/20/2010 8:26:01 AM
   66 Result: The task completed with an exit code of (6).
"77 Verify Informix Online.job" (perl.exe)
   77 Finished 9/20/2010 8:27:01 AM
   77 Result: The task completed with an exit code of (7).
"88 TEST123 - Monitor MQ Directories.job" (perl)
   88 Finished 9/20/2010 8:28:01 AM
   88 Result: The task completed with an exit code of (8).
"00 TEST123 - Monitor MQ Directories.job" (perl)
   00 Finished 9/20/2010 8:28:01 AM
   00 Result: The task completed with an exit code of (0).


Expected results are below
Code:
"01 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   01 Result: The task completed with an exit code of (1).
"11 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   11 Finished 9/20/2010 8:21:00 AM
   11 More 9/20/2010 8:21:00 AM
   11 MoreMore 9/20/2010 8:21:00 AM
   11 Result: The task completed with an exit code of (1).
"22 TEST123 - Raise Alert for Rejected ACK for WBC.job" (perl)
   22 Finished 9/20/2010 8:22:00 AM
   22 Result: The task completed with an exit code of (2).
"33 TEST123 - VIF Sanity Script Alert.job" (bash.exe)
   33 Finished 9/20/2010 8:23:00 AM
   33 Result: The task completed with an exit code of (3).
"44 TEST123 - Copy Secure Script Bash.job" (bash.exe)
   44 Finished 9/20/2010 8:24:00 AM
   44Result: The task completed with an exit code of (4).
"55 TEST123 - VIF Sanity Script.job" (bash.exe)
   55 Finished 9/20/2010 8:25:00 AM
   55 Result: The task completed with an exit code of (5).
"66 Storage Space Monitor.job" (perl.exe)
   66 Finished 9/20/2010 8:26:01 AM
   66 Result: The task completed with an exit code of (6).
"77 Verify Informix Online.job" (perl.exe)
   77 Finished 9/20/2010 8:27:01 AM
   77 Result: The task completed with an exit code of (7).
"88 TEST123 - Monitor MQ Directories.job" (perl)
   88 Finished 9/20/2010 8:28:01 AM
   88 Result: The task completed with an exit code of (8).

What I am getting is below
Code:
"01 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   01 Result: The task completed with an exit code of (1).
   11 MoreMore 9/20/2010 8:21:00 AM
   11 More 9/20/2010 8:21:00 AM
   11 Finished 9/20/2010 8:21:00 AM
"11 TEST123 - Move_NT_Acks_To_Darwin.job" (TEST123 - Move_NT_Acks_To_Darwin.bat)
   11 Result: The task completed with an exit code of (1).
   22 Finished 9/20/2010 8:22:00 AM
"22 TEST123 - Raise Alert for Rejected ACK for WBC.job" (perl)
   22 Result: The task completed with an exit code of (2).
   33 Finished 9/20/2010 8:23:00 AM
"33 TEST123 - VIF Sanity Script Alert.job" (bash.exe)
   33 Result: The task completed with an exit code of (3).
   44 Finished 9/20/2010 8:24:00 AM
"44 TEST123 - Copy Secure Script Bash.job" (bash.exe)
   44Result: The task completed with an exit code of (4).
   55 Finished 9/20/2010 8:25:00 AM
"55 TEST123 - VIF Sanity Script.job" (bash.exe)
   55 Result: The task completed with an exit code of (5).
   66 Finished 9/20/2010 8:26:01 AM
"66 Storage Space Monitor.job" (perl.exe)
   66 Result: The task completed with an exit code of (6).
   77 Finished 9/20/2010 8:27:01 AM
"77 Verify Informix Online.job" (perl.exe)
   77 Result: The task completed with an exit code of (7).
   88 Finished 9/20/2010 8:28:01 AM
"88 TEST123 - Monitor MQ Directories.job" (perl)
   88 Result: The task completed with an exit code of (8).


As you can see, the actual results do not appear in the right order.

Any help in a solution, and stopping me going crazy, is greatly appreciated. Thanks

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2010, 5:07 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
As you've probably guessed, you'll need to keep some of the lines in another variable as they are read from the file. Assuming you have other reasons for using the file reading loop as opposed to loading the file into memory and simply parsing it by jobs, consider this code:
Code:
Loop, Read, %MyFile%
{
    If RegexMatch( A_LoopReadLine, "^""\K[^""]+(?="")", match )
        ThisBlock := A_LoopReadLine, ThisProg := match
    Else ThisBlock .= "`n" . A_LoopReadLine
    If RegexMatch( A_LoopReadLine, "exit code of \((\d+)\)", ExitCode ) && ExitCode
        MsgBox, The task "%ThisProg%" failed with an exit code of %ExitCode%`n`n%ThisBlock%
}

As it reads the file, the variable 'ThisBlock' accumulates the lines, but is cleared when a new job is encountered.

Does this help?

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 24th, 2010, 5:54 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
Hi and thanks for the quick response.

It looks great. I was staring at my code forever and getting nowhere.

Thanks again.

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: 0x150||ISO, batto, Bing [Bot], dra, HotkeyStick, mKnight, sjc1000, Wicked, XstatyK and 63 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group