Jump to content

parsing text from a log file to a GUI Label


gr1fter
 Share

Recommended Posts

Hello,

Hopefully this is simple for others, but i don't even know where to start with this one. Any push in the right direction would be great. I would like to take info that is being place in my log file from USMT and pass it to a label in my GUI.

info in my log looks like this:

27 Sep 2010, 14:36:59 -04:00, 00:01:10, totalPercentageCompleted, 5

27 Sep 2010, 14:37:01 -04:00, 00:01:12, totalPercentageCompleted, 6

27 Sep 2010, 14:37:02 -04:00, 00:01:13, totalPercentageCompleted, 7

27 Sep 2010, 14:37:03 -04:00, 00:01:14, totalPercentageCompleted, 8

27 Sep 2010, 14:37:04 -04:00, 00:01:15, totalPercentageCompleted, 9

27 Sep 2010, 14:37:05 -04:00, 00:01:16, totalPercentageCompleted, 10

27 Sep 2010, 14:37:06 -04:00, 00:01:17, totalPercentageCompleted, 11

27 Sep 2010, 14:37:08 -04:00, 00:01:19, totalPercentageCompleted, 12

27 Sep 2010, 14:37:11 -04:00, 00:01:22, totalPercentageCompleted, 13

27 Sep 2010, 14:37:12 -04:00, 00:01:22, PHASE, Saving

27 Sep 2010, 14:37:13 -04:00, 00:01:24, totalPercentageCompleted, 14

27 Sep 2010, 14:37:14 -04:00, 00:01:25, totalPercentageCompleted, 15

27 Sep 2010, 14:37:16 -04:00, 00:01:27, totalPercentageCompleted, 16

27 Sep 2010, 14:37:19 -04:00, 00:01:30, totalPercentageCompleted, 17

27 Sep 2010, 14:37:22 -04:00, 00:01:33, totalPercentageCompleted, 18

27 Sep 2010, 14:37:26 -04:00, 00:01:37, totalPercentageCompleted, 19

27 Sep 2010, 14:37:30 -04:00, 00:01:41, totalPercentageCompleted, 20

27 Sep 2010, 14:37:31 -04:00, 00:01:42, totalPercentageCompleted, 21

27 Sep 2010, 14:37:34 -04:00, 00:01:45, totalPercentageCompleted, 22

27 Sep 2010, 14:37:38 -04:00, 00:01:49, totalPercentageCompleted, 23

27 Sep 2010, 14:37:42 -04:00, 00:01:53, totalPercentageCompleted, 24

27 Sep 2010, 14:37:45 -04:00, 00:01:55, totalPercentageCompleted, 25

27 Sep 2010, 14:37:48 -04:00, 00:01:59, totalPercentageCompleted, 26

27 Sep 2010, 14:37:52 -04:00, 00:02:02, totalPercentageCompleted, 27

27 Sep 2010, 14:37:53 -04:00, 00:02:04, totalPercentageCompleted, 28

27 Sep 2010, 14:37:57 -04:00, 00:02:08, totalPercentageCompleted, 29

27 Sep 2010, 14:37:58 -04:00, 00:02:09, totalMinutesRemaining, 6

27 Sep 2010, 14:38:02 -04:00, 00:02:13, totalPercentageCompleted, 30

27 Sep 2010, 14:38:04 -04:00, 00:02:14, totalPercentageCompleted, 31

27 Sep 2010, 14:38:07 -04:00, 00:02:18, totalPercentageCompleted, 32

27 Sep 2010, 14:38:10 -04:00, 00:02:21, totalPercentageCompleted, 33

27 Sep 2010, 14:38:13 -04:00, 00:02:24, totalPercentageCompleted, 34

27 Sep 2010, 14:38:15 -04:00, 00:02:25, totalPercentageCompleted, 35

27 Sep 2010, 14:38:16 -04:00, 00:02:27, totalPercentageCompleted, 36

27 Sep 2010, 14:38:18 -04:00, 00:02:29, totalPercentageCompleted, 37

27 Sep 2010, 14:38:19 -04:00, 00:02:30, totalMinutesRemaining, 5

27 Sep 2010, 14:38:20 -04:00, 00:02:31, totalPercentageCompleted, 38

27 Sep 2010, 14:38:23 -04:00, 00:02:33, totalPercentageCompleted, 39

27 Sep 2010, 14:38:24 -04:00, 00:02:35, totalPercentageCompleted, 40

27 Sep 2010, 14:38:26 -04:00, 00:02:37, totalPercentageCompleted, 41

27 Sep 2010, 14:38:28 -04:00, 00:02:39, totalPercentageCompleted, 42

27 Sep 2010, 14:38:31 -04:00, 00:02:42, totalPercentageCompleted, 43

27 Sep 2010, 14:38:33 -04:00, 00:02:44, totalPercentageCompleted, 44

27 Sep 2010, 14:38:35 -04:00, 00:02:45, totalPercentageCompleted, 45

27 Sep 2010, 14:38:37 -04:00, 00:02:48, totalPercentageCompleted, 46

27 Sep 2010, 14:38:41 -04:00, 00:02:52, totalPercentageCompleted, 47

27 Sep 2010, 14:38:44 -04:00, 00:02:54, totalMinutesRemaining, 4

27 Sep 2010, 14:38:44 -04:00, 00:02:55, totalPercentageCompleted, 48

27 Sep 2010, 14:38:48 -04:00, 00:02:58, totalPercentageCompleted, 49

27 Sep 2010, 14:38:50 -04:00, 00:03:01, totalPercentageCompleted, 50

27 Sep 2010, 14:38:54 -04:00, 00:03:04, totalPercentageCompleted, 51

27 Sep 2010, 14:38:58 -04:00, 00:03:09, totalPercentageCompleted, 52

27 Sep 2010, 14:39:02 -04:00, 00:03:13, totalPercentageCompleted, 53

27 Sep 2010, 14:39:04 -04:00, 00:03:15, totalPercentageCompleted, 54

27 Sep 2010, 14:39:05 -04:00, 00:03:16, totalPercentageCompleted, 55

27 Sep 2010, 14:39:07 -04:00, 00:03:17, totalPercentageCompleted, 56

27 Sep 2010, 14:39:08 -04:00, 00:03:19, totalPercentageCompleted, 57

27 Sep 2010, 14:39:10 -04:00, 00:03:21, totalPercentageCompleted, 58

27 Sep 2010, 14:39:11 -04:00, 00:03:22, totalPercentageCompleted, 59

27 Sep 2010, 14:39:13 -04:00, 00:03:24, totalPercentageCompleted, 60

27 Sep 2010, 14:39:14 -04:00, 00:03:25, totalPercentageCompleted, 61

27 Sep 2010, 14:39:17 -04:00, 00:03:28, totalPercentageCompleted, 62

27 Sep 2010, 14:39:19 -04:00, 00:03:30, totalMinutesRemaining, 3

27 Sep 2010, 14:39:20 -04:00, 00:03:31, totalPercentageCompleted, 63

27 Sep 2010, 14:39:22 -04:00, 00:03:33, totalPercentageCompleted, 64

27 Sep 2010, 14:39:26 -04:00, 00:03:36, totalPercentageCompleted, 65

27 Sep 2010, 14:39:30 -04:00, 00:03:41, totalPercentageCompleted, 66

27 Sep 2010, 14:39:33 -04:00, 00:03:44, totalPercentageCompleted, 67

27 Sep 2010, 14:39:34 -04:00, 00:03:45, totalPercentageCompleted, 68

27 Sep 2010, 14:39:35 -04:00, 00:03:46, totalPercentageCompleted, 69

27 Sep 2010, 14:39:37 -04:00, 00:03:48, totalPercentageCompleted, 70

27 Sep 2010, 14:39:40 -04:00, 00:03:50, totalPercentageCompleted, 71

27 Sep 2010, 14:39:43 -04:00, 00:03:54, totalPercentageCompleted, 72

27 Sep 2010, 14:39:46 -04:00, 00:03:57, totalPercentageCompleted, 73

27 Sep 2010, 14:39:48 -04:00, 00:03:59, totalPercentageCompleted, 74

27 Sep 2010, 14:39:52 -04:00, 00:04:03, totalPercentageCompleted, 75

27 Sep 2010, 14:39:54 -04:00, 00:04:05, totalPercentageCompleted, 76

27 Sep 2010, 14:39:55 -04:00, 00:04:06, totalPercentageCompleted, 77

27 Sep 2010, 14:39:56 -04:00, 00:04:07, totalPercentageCompleted, 78

27 Sep 2010, 14:39:57 -04:00, 00:04:08, totalPercentageCompleted, 79

27 Sep 2010, 14:39:58 -04:00, 00:04:09, totalPercentageCompleted, 80

27 Sep 2010, 14:39:59 -04:00, 00:04:09, totalMinutesRemaining, 2

27 Sep 2010, 14:39:59 -04:00, 00:04:10, totalPercentageCompleted, 81

27 Sep 2010, 14:40:01 -04:00, 00:04:11, totalPercentageCompleted, 82

27 Sep 2010, 14:40:02 -04:00, 00:04:13, totalPercentageCompleted, 83

27 Sep 2010, 14:40:05 -04:00, 00:04:16, totalPercentageCompleted, 84

27 Sep 2010, 14:40:07 -04:00, 00:04:18, totalPercentageCompleted, 85

27 Sep 2010, 14:40:09 -04:00, 00:04:20, totalPercentageCompleted, 86

27 Sep 2010, 14:40:12 -04:00, 00:04:23, totalPercentageCompleted, 87

27 Sep 2010, 14:40:15 -04:00, 00:04:26, totalPercentageCompleted, 88

27 Sep 2010, 14:40:17 -04:00, 00:04:28, totalPercentageCompleted, 89

27 Sep 2010, 14:40:17 -04:00, 00:04:28, totalPercentageCompleted, 90

27 Sep 2010, 14:40:20 -04:00, 00:04:31, totalPercentageCompleted, 91

27 Sep 2010, 14:40:22 -04:00, 00:04:33, totalPercentageCompleted, 92

27 Sep 2010, 14:40:24 -04:00, 00:04:35, totalPercentageCompleted, 93

27 Sep 2010, 14:40:28 -04:00, 00:04:39, totalPercentageCompleted, 94

27 Sep 2010, 14:40:31 -04:00, 00:04:42, totalPercentageCompleted, 100

I would like to read this file while it is getting populated and take the following:

totalPercentageCompleted, x --> to a Label in my GUI that says "Percent Complete = x"

totalMinutesRemaining, x --> to a Label in my GUI that says "Minutes Remaining = x"

I have searched the forums and searching and reading text is still very confusing to me. Any help would be great. Thank You

Edit:

Example I started working, Im not sure if im going the right way though ;)

#include <file.au3>

#include <GUIConstantsEx.au3>







Dim $readrecord



$GUI = GUICreate("", 430, 50, -1, -1, -1)

$example = GUICtrlCreateLabel ("This is Example - ",10, 25, 430,Default)

GUISetBkColor(0xFFFFFF)

GUISetState(@SW_ENABLE)

GUISetState(@SW_SHOW)



_FileReadToArray("d:\example.log",$readrecord)



For $x = 1 to $readrecord[0]

    GUICtrlSetData($example,$readrecord)

Next





MsgBox (48,"",$readrecord[1])

Example()



Func Example()

    Local $msg

; Run the GUI until the dialog is closed

    While 1

        $msg = GUIGetMsg()

        Select

        Case $msg = $GUI_EVENT_CLOSE

            Exit

        EndSelect

    WEnd

EndFunc   ;==>Example
Edited by gr1fter
Link to comment
Share on other sites

I'm not quite sure what you're trying to do. Firstly this part of the code is wrong for a couple of reasons.

For $x = 1 to $readrecord[0]
   GUICtrlSetData($example,$readrecord) ; <=
Next

You need to add the element $x like this:

For $x = 1 to $readrecord[0] Step 2 ; Step 2 because there are empty lines in the log file.
   Sleep(600)
   GUICtrlSetData($example,$readrecord[$x]) ; <=
Next

However I don't see why you would set all those values in such a loop. Only the final value will display.

Edit

Actually the loop is fine, sorry about that. I just added a sleep time interval to make sure it was working correctly. See modification above.

Edited by czardas
Link to comment
Share on other sites

I'm not quite sure what you're trying to do. Firstly this part of the code is wrong for a couple of reasons.

For $x = 1 to $readrecord[0]
   GUICtrlSetData($example,$readrecord) ; <=
Next

You need to add the element $x like this:

For $x = 1 to $readrecord[0] Step 2 ; Step 2 because there are empty lines in the log file.
   Sleep(600)
   GUICtrlSetData($example,$readrecord[$x]) ; <=
Next

However I don't see why you would set all those values in such a loop. Only the final value will display.

Edit

Actually the loop is fine, sorry about that. I just added a sleep time interval to get it going. See modification above.

Yes that is almost the way i want it to work! Now I would like to take the string and modify it so the label will show:"Total Percent Complete = 6"

Pretty Much take - 27 Sep 2010, 14:37:01 -04:00, 00:01:12, totalPercentageCompleted, 6 and convert it to "Total Percent Complete = 6"

I could do a StringRight and grab the last digit and make it a variable? But how would I eliminate the lines I don't want like:

27 Sep 2010, 14:37:12 -04:00, 00:01:22, PHASE, Saving

27 Sep 2010, 14:38:19 -04:00, 00:02:30, totalMinutesRemaining, 5

Thank You

Link to comment
Share on other sites

Instead of using FileReadToArray you could use this. At least it seems to work with the example you gave, when it is just one empty line inbetween each time. However if the number of empty lines changes then you could use StringRegExpReplace instead.

Local $readrecord, $s_FileData = FileRead("example.log")
$readrecord = StringSplit($s_FileData, @CRLF & @CRLF, 1)

I appears I misunderstood your question. I'm a bit tired right now. ;) I'm not sure which lines you want to eliminate.

If you mean don't display certain lines then why eliminate them. Just don't display them on the label. You can test the contents of the string using StringInStr or StringRegExp. I'm no expert in RegExp.

Maybe something like this is all you need.

Local $sRtnString, $sTestStr = "27 Sep 2010, 14:37:01 -04:00, 00:01:12, totalPercentageCompleted, 6"
If StringInStr($sTestStr, "totalPercentageCompleted") Then
    $sRtnString = "Total Percent Complete = " &StringRight($sTestStr, 1)
    MsgBox(0, "", $sRtnString)
EndIf

Arghh The numbers can be two or three digits, so it won't work.

Edited by czardas
Link to comment
Share on other sites

I find RegExp soo hard. This does grab the number from the end of the string, but I don't know why. What I do know is that it's wrong. ;)

Local $sTestStr = "27 Sep 2010, 14:37:01 -04:00, 00:01:12, totalPercentageCompleted, 25"
Local $sScrap = StringRegExpReplace($sTestStr, "(?s)[\s][\d]+\z", "")
$sTestStr = StringReplace($sTestStr, $sScrap , "") ; This line should not be necessary with the correct RegExp!
MsgBox(0, "$sTestStr", $sTestStr)

So hopefully somebody will tell me what is wrong with the RegExp.

Edited by czardas
Link to comment
Share on other sites

I find RegExp soo hard. This does grab the number from the end of the string, but I don't know why. What I do know is that it's wrong. ;)

Local $sTestStr = "27 Sep 2010, 14:37:01 -04:00, 00:01:12, totalPercentageCompleted, 25"
Local $sScrap = StringRegExpReplace($sTestStr, "(?s)[\s][\d]+\z", "")
$sTestStr = StringReplace($sTestStr, $sScrap , "") ; This line should not be necessary with the correct RegExp!
MsgBox(0, "$sTestStr", $sTestStr)

So hopefully somebody will tell me what is wrong with the RegExp.

I took some of your code from both of your suggestions and combined somethings and I think I have it working. This is what I came up with and it seems to be reading and outputting the file correctly.

#include <file.au3>
#include <GUIConstantsEx.au3>




Dim $readrecord,$strPercent,$strMinutes,$sStrip

$GUI = GUICreate("", 430, 50, -1, -1, -1)
$example = GUICtrlCreateLabel ("Total Percentage Complete:",10, 5, 430,Default)
$example2 = GUICtrlCreateLabel ("Total Minutes Remaining:",10, 30, 430,Default)
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_ENABLE)
GUISetState(@SW_SHOW)

_FileReadToArray("d:\example.log",$readrecord)


For $x = 1 to $readrecord[0] Step 1

    $strPercent = StringInStr($readrecord[$x],"totalPercentageCompleted")
    $strMinutes = StringInStr($readrecord[$x],"totalMinutesRemaining")
    $sStrip = StringRight($readrecord[$x], 2)

    If $strPercent <> 0 Then
        Sleep(600)
        GUICtrlSetData($example,"Total Percentage Complete: " & $sStrip & "%") ; <=
    ElseIf $strMinutes <> 0 Then
        Sleep(600)
        GUICtrlSetData($example2,"Total Minutes Remaining: " & $sStrip) ; <=
    Else
    EndIf

Next

Example()

Func Example()
    Local $msg
; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        EndSelect
    WEnd
EndFunc   ;==>Example

I can count all the way to 100% but since I can only StringRight 2 characters it is sending the output as 00%. Is there any way I can stop the array at 99% so I don't have to worry about 3 digits? or anything else I can do?

Much appreciate the help, Thank You!

Link to comment
Share on other sites

I'm glad you got it working. Hopefully I'll find the time to study SRE properly sometime. I think I need to set aside a week to figure out the syntax. Without using SRE you could also use something like the following, which will display 100% instead of 00% at the end. And it is perfectly acceptable.

#include <file.au3>
#include <GUIConstantsEx.au3>

Dim $readrecord,$strPercent,$strMinutes,$sStrip

$GUI = GUICreate("", 430, 50, -1, -1, -1)
$example = GUICtrlCreateLabel ("Total Percentage Complete:",10, 5, 430,Default)
$example2 = GUICtrlCreateLabel ("Total Minutes Remaining:",10, 30, 430,Default)
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_ENABLE)
GUISetState(@SW_SHOW)

_FileReadToArray("d:\example.log",$readrecord)


For $x = 1 to $readrecord[0] Step 1

    $strPercent = StringInStr($readrecord[$x],"totalPercentageCompleted")
    $strMinutes = StringInStr($readrecord[$x],"totalMinutesRemaining")
    $sStrip = StringStripWS(StringReplace(StringRight($readrecord[$x], 3), ",", ""), 1) ; Instead of SRE.

    If $strPercent <> 0 Then
        Sleep(600)
        GUICtrlSetData($example,"Total Percentage Complete: " & $sStrip & "%") ; <=
    ElseIf $strMinutes <> 0 Then
        Sleep(600)
        GUICtrlSetData($example2,"Total Minutes Remaining: " & $sStrip) ; <=
    Else
    EndIf

Next

Example()

Func Example()
    Local $msg
; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        EndSelect
    WEnd
EndFunc   ;==>Example

Edit

I forgot that I changed the path to the file. I just changed it back again. ;)

Edited by czardas
Link to comment
Share on other sites

czardas,

That worked great, Thank YOU! ;)

I now tried to use this script while the program was actually running and generating the log file and ran into a couple issues. I now do not need to use the _FileReadToArray("d:\example.log",$readrecord) as I really only need to read the last line of the log file while it is getting generated.

So i changed the code to read the last line and check the log file every 5 seconds with adlibregister:

#include <file.au3>

#include <GUIConstantsEx.au3>



Dim $readrecord,$strPercent,$strMinutes,$sStrip



$GUI = GUICreate("", 430, 50, -1, -1, -1)

$example = GUICtrlCreateLabel ("Total Percentage Complete:",10, 5, 430,Default)

$example2 = GUICtrlCreateLabel ("Total Minutes Remaining:",10, 30, 430,Default)

GUISetBkColor(0xFFFFFF)

GUISetState(@SW_ENABLE)

GUISetState(@SW_SHOW)



AdlibRegister ("progressinfo",5000)



Example()



Func Example()

    Local $msg

; Run the GUI until the dialog is closed

    While 1

        $msg = GUIGetMsg()

        Select

        Case $msg = $GUI_EVENT_CLOSE

            Exit

        EndSelect

    WEnd

EndFunc   ;==>Example





Func progressinfo()



Filecopy ("D:\example.log","D:\example2.log",1)





$file = FileOpen ("D:\example2.log",0)



; Check if file opened for reading OK

    If $file = -1 Then

        MsgBox(0, "Error", "Unable to open file.")

    EndIf





$line = FileReadLine($file,-1)





$strPercent = StringInStr($line,"totalPercentageCompleted")

$strMinutes = StringInStr($line,"totalMinutesRemaining")

$sStrip = StringStripWS(StringReplace(StringRight($line, 3), ",", ""), 1) ; Instead of SRE.



    If $strPercent <> 0 Then

        Sleep(600)

        GUICtrlSetData($example,"Total Percentage Complete: " & $sStrip & "%") ; <=

    ElseIf $strMinutes <> 0 Then

        Sleep(600)

        GUICtrlSetData($example2,"Total Minutes Remaining: " & $sStrip) ; <=

    EndIf



EndFunc

I was curious if there is any way I can read the log file without copying it first, it seems to be locked when i just do a straight fileread? but if i do a filecopy every time, it works just the way i want it.

Edited by gr1fter
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...