Sign in to follow this  
Followers 0
iceberg

2 problems

3 posts in this topic

I have 2 questions:

1) my problem lies in the While Loop statements (Line 31) : how do I ensure that the script gets the current "LastEOF" and the current date? Because I have declared them outside the while loop statements. not too sure how to cut and paste them inside the while loop cleanly. and also, not too sure if I can use GLOBAL inside a while statement.

2) is there anything wrong with the Global $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", "LastEOF")) in line 22? If I don't use the while loop and exit the script after the frist run, the get the following results :

last EOF = 22 lines

current EOF = 30 lines

diff = 8

but i only get 7, meaning it starts reading from line 24 and not line 23.

pls help me. thanks.

#include <Date.au3>
#include <Constants.au3>
Opt("TrayAutoPause", 0)
TraySetState()

If Not FileExists(@ScriptDir & "\settings.ini") Then
    FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    Exit
EndIf

Global $inifile = "settings.ini"
Global $YYMMDD = StringMid(@YEAR & @MON & @MDAY, 3)
Global $sTextFound = ""
Global $nMonitorSleep = Number((IniRead($inifile, "config", "delay", "delay")) * 60000)
Global $sFileLocationName = (IniRead($inifile, "config", "FilePath", "FilePath")) & $YYMMDD & ".msg"
HotKeySet("{F7}", "LeaveLoop")
HotKeySet("{F8}", "endall")
Global $ll, $kk = False
Global $work = True
Global $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", "LastEOF"))
Dim $array

$Date = IniRead($inifile, "config", "date", "date")
If $Date <> $YYMMDD Then
    IniWrite($inifile, "config", "date", $YYMMDD)
    IniWrite($inifile, "config", "LastEOF", "0")
EndIf

While 1
    If $work Then
        $work = False
        MonitorChanges()
        AdlibEnable("setgoing", $nMonitorSleep)
    EndIf
    If $kk Then ExitLoop
    Sleep(100)
WEnd

Func setgoing()
    $work = True
    AdlibDisable()
EndFunc  ;==>setgoing

Func LeaveLoop()
    $ll = True
EndFunc  ;==>LeaveLoop

Func endall()
    $ll = True
    $kk = True
EndFunc  ;==>endall

Func MonitorChanges()
    $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", ''))
    $ll = False

    If FileOpen($sFileLocationName, 0) = -1 Then
        MsgBox(48, "Error!", "Log file " & $YYMMDD & ".msg could not be opened. " & @CRLF & @CRLF _
                 & "1. Enter a valid path in settings.ini" & @CRLF _
                 & "2. Check if the file exists" & @CRLF _
                 & "3. Make sure you have access to the file" & @CRLF _
                 & "4. Check if the file is not opened by another program/script", 10)
        FileWrite(@ScriptDir & "\error.log", "Log file error on " & _Now() & @CRLF)
        Exit
    EndIf

    $sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)
    If $sTextFound <> "" Then
        If StringInStr($sTextFound, (IniRead($inifile, "config", "search", "search"))) Then
            $found = 0
            For $x = 0 To UBound($array) - 1
                If $sTextFound == $array[$x] Then
                    $found = 1
                    ExitLoop
                EndIf
            Next
            If Not $found Then
                If IsArray($array) Then
                    ReDim $array[UBound($array) + 1]
                Else
                    Dim $array[1]
                EndIf
                $array[UBound($array) - 1] = $sTextFound
            EndIf
        EndIf
    EndIf
    If $ll Then Exit
    AppendText($array)
    IniWrite($inifile, "config", "LastEOF", $nLastEOF)
    FileClose($sFileLocationName)
EndFunc  ;==>MonitorChanges

Func AppendText($aT)
    For $x = 0 To UBound($aT) - 1
        $ans = StringRegExp($array[$x], ".*" & (IniRead($inifile, "config", "search", "search")) & ".*", 3)
        For $r = 0 To UBound($ans) - 1
            If Not FileExists(@TempDir & "\ICT\") Then DirCreate(@TempDir & "\ICT\")
            FileWrite(@TempDir & "\ICT\" & $YYMMDD & ".txt", $ans[$r] & @CRLF)
            IniWrite($inifile, "config", "LastEOF", $nLastEOF)
        Next
    Next
    Zedna()
EndFunc  ;==>AppendText

Func _FileMonitorLog($sFile, ByRef $nLastEOF)
    Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)
    Local $sLog = ""
    If ($aSplit[0] > $nLastEOF) Then
        For $iCC = $nLastEOF + 1 To $aSplit[0]
            $sLog &= $aSplit[$iCC] & @CRLF
        Next
        $nLastEOF = $aSplit[0]
    EndIf
    Return $sLog
EndFunc  ;==>_FileMonitorLog

Func Zedna()
    $Found20 = "No"
    $TxtFile20 = StringMid(@YEAR & @MON & @MDAY, 3)
    $data20 = FileRead(@TempDir & "\ICT\" & $TxtFile20 & ".txt")

    If @error Then
        MsgBox(48, "Error!", "File could not be opened. " & @CRLF _
                 & "1. Enter a valid path" & @CRLF _
                 & "2. Check if the file exists" & @CRLF _
                 & "3. Make sure you have access to the file" & @CRLF _
                 & "4. Check if the file is not opened by another program/script" & @CRLF _
                 & "5. Current search did not find any matches to generate results", 10)
        FileWrite(@ScriptDir & "\error.log", "Search file error on " & _Now() & @CRLF)
    Else

        $data20 = StringSplit($data20, @CRLF, 1)

        For $i = 1 To $data20[0]
            $Line20 = $data20[$i]
            $array20 = StringSplit($Line20, @TAB)
            If $array20[0] > 11 And StringInStr($array20[12], (IniRead($inifile, "config", "search", "search"))) Then
                $Found20 = "Yes"
                $3 = StringTrimLeft($array20[3], 1)
                $8 = StringTrimRight($array20[8], 6)
                $13 = StringTrimRight($array20[13], 2)
                FileWrite((IniRead($inifile, "config", "sendto", "sendto")) & $3 & ".txt", $8 & @TAB)
                FileWrite((IniRead($inifile, "config", "sendto", "sendto")) & $3 & ".txt", $13 & @CRLF)
            EndIf
        Next

        FileDelete(@TempDir & "\ICT\" & $YYMMDD & ".txt")

        If $Found20 = "No" Then
            MsgBox(0, "Not found!", "Script could not find the string you were searching for.")
        EndIf

    EndIf
    
    $Time = _DateAdd('n', (IniRead($inifile, "config", "delay", "delay")), _NowCalc())
    MsgBox(4096, "", "Script will perform next search on " & $Time, 10)
    TraySetToolTip("CCG: F8 to exit. Next search on " & $Time)
EndFunc  ;==>Zedna

mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites



I have 2 questions:

1) my problem lies in the While Loop statements (Line 31) : how do I ensure that the script gets the current "LastEOF" and the current date? Because I have declared them outside the while loop statements. not too sure how to cut and paste them inside the while loop cleanly. and also, not too sure if I can use GLOBAL inside a while statement.

Global/Local is in the context of inside/outside Functions, not loops.

2) is there anything wrong with the Global $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", "LastEOF")) in line 22?

There is nothing wrong with that line. The default value of "LastEOF" doesn't make sense, since the Number() conversion will just make that "0" anyway.

If I don't use the while loop and exit the script after the frist run, the get the following results :

last EOF = 22 lines

current EOF = 30 lines

diff = 8

but i only get 7, meaning it starts reading from line 24 and not line 23.

pls help me. thanks.

CODE
#include <Date.au3>

#include <Constants.au3>

Opt("TrayAutoPause", 0)

TraySetState()

If Not FileExists(@ScriptDir & "\settings.ini") Then

FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)

MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")

Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)

Exit

EndIf

Global $inifile = "settings.ini"

Global $YYMMDD = StringMid(@YEAR & @MON & @MDAY, 3)

Global $sTextFound = ""

Global $nMonitorSleep = Number((IniRead($inifile, "config", "delay", "delay")) * 60000)

Global $sFileLocationName = (IniRead($inifile, "config", "FilePath", "FilePath")) & $YYMMDD & ".msg"

HotKeySet("{F7}", "LeaveLoop")

HotKeySet("{F8}", "endall")

Global $ll, $kk = False

Global $work = True

Global $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", "LastEOF"))

Dim $array

$Date = IniRead($inifile, "config", "date", "date")

If $Date <> $YYMMDD Then

IniWrite($inifile, "config", "date", $YYMMDD)

IniWrite($inifile, "config", "LastEOF", "0")

EndIf

While 1

If $work Then

$work = False

MonitorChanges()

AdlibEnable("setgoing", $nMonitorSleep)

EndIf

If $kk Then ExitLoop

Sleep(100)

WEnd

Func setgoing()

$work = True

AdlibDisable()

EndFunc ;==>setgoing

Func LeaveLoop()

$ll = True

EndFunc ;==>LeaveLoop

Func endall()

$ll = True

$kk = True

EndFunc ;==>endall

Func MonitorChanges()

$nLastEOF = Number(IniRead($inifile, "config", "LastEOF", ''))

$ll = False

If FileOpen($sFileLocationName, 0) = -1 Then

MsgBox(48, "Error!", "Log file " & $YYMMDD & ".msg could not be opened. " & @CRLF & @CRLF _

& "1. Enter a valid path in settings.ini" & @CRLF _

& "2. Check if the file exists" & @CRLF _

& "3. Make sure you have access to the file" & @CRLF _

& "4. Check if the file is not opened by another program/script", 10)

FileWrite(@ScriptDir & "\error.log", "Log file error on " & _Now() & @CRLF)

Exit

EndIf

$sTextFound = _FileMonitorLog($sFileLocationName, $nLastEOF)

If $sTextFound <> "" Then

If StringInStr($sTextFound, (IniRead($inifile, "config", "search", "search"))) Then

$found = 0

For $x = 0 To UBound($array) - 1

If $sTextFound == $array[$x] Then

$found = 1

ExitLoop

EndIf

Next

If Not $found Then

If IsArray($array) Then

ReDim $array[uBound($array) + 1]

Else

Dim $array[1]

EndIf

$array[uBound($array) - 1] = $sTextFound

EndIf

EndIf

EndIf

If $ll Then Exit

AppendText($array)

IniWrite($inifile, "config", "LastEOF", $nLastEOF)

FileClose($sFileLocationName)

EndFunc ;==>MonitorChanges

Func AppendText($aT)

For $x = 0 To UBound($aT) - 1

$ans = StringRegExp($array[$x], ".*" & (IniRead($inifile, "config", "search", "search")) & ".*", 3)

For $r = 0 To UBound($ans) - 1

If Not FileExists(@TempDir & "\ICT\") Then DirCreate(@TempDir & "\ICT\")

FileWrite(@TempDir & "\ICT\" & $YYMMDD & ".txt", $ans[$r] & @CRLF)

IniWrite($inifile, "config", "LastEOF", $nLastEOF)

Next

Next

Zedna()

EndFunc ;==>AppendText

Func _FileMonitorLog($sFile, ByRef $nLastEOF)

Local $aSplit = StringSplit(StringStripCR(FileRead($sFile)), @LF)

Local $sLog = ""

If ($aSplit[0] > $nLastEOF) Then

For $iCC = $nLastEOF + 1 To $aSplit[0]

$sLog &= $aSplit[$iCC] & @CRLF

Next

$nLastEOF = $aSplit[0]

EndIf

Return $sLog

EndFunc ;==>_FileMonitorLog

Func Zedna()

$Found20 = "No"

$TxtFile20 = StringMid(@YEAR & @MON & @MDAY, 3)

$data20 = FileRead(@TempDir & "\ICT\" & $TxtFile20 & ".txt")

If @error Then

MsgBox(48, "Error!", "File could not be opened. " & @CRLF _

& "1. Enter a valid path" & @CRLF _

& "2. Check if the file exists" & @CRLF _

& "3. Make sure you have access to the file" & @CRLF _

& "4. Check if the file is not opened by another program/script" & @CRLF _

& "5. Current search did not find any matches to generate results", 10)

FileWrite(@ScriptDir & "\error.log", "Search file error on " & _Now() & @CRLF)

Else

$data20 = StringSplit($data20, @CRLF, 1)

For $i = 1 To $data20[0]

$Line20 = $data20[$i]

$array20 = StringSplit($Line20, @TAB)

If $array20[0] > 11 And StringInStr($array20[12], (IniRead($inifile, "config", "search", "search"))) Then

$Found20 = "Yes"

$3 = StringTrimLeft($array20[3], 1)

$8 = StringTrimRight($array20[8], 6)

$13 = StringTrimRight($array20[13], 2)

FileWrite((IniRead($inifile, "config", "sendto", "sendto")) & $3 & ".txt", $8 & @TAB)

FileWrite((IniRead($inifile, "config", "sendto", "sendto")) & $3 & ".txt", $13 & @CRLF)

EndIf

Next

FileDelete(@TempDir & "\ICT\" & $YYMMDD & ".txt")

If $Found20 = "No" Then

MsgBox(0, "Not found!", "Script could not find the string you were searching for.")

EndIf

EndIf

$Time = _DateAdd('n', (IniRead($inifile, "config", "delay", "delay")), _NowCalc())

MsgBox(4096, "", "Script will perform next search on " & $Time, 10)

TraySetToolTip("CCG: F8 to exit. Next search on " & $Time)

EndFunc ;==>Zedna

The only place you change $LastEOF is inside the _FileMonitorLog() function, where it is used ByRef. Since it was already Global, ByRef wasn't necessary, but it shouldn't hurt anything.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

PsaltyDS, thanks for the elaboration.


mouse not found....scroll any mouse to continue.

Share this post


Link to post
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
Sign in to follow this  
Followers 0