Jump to content

Need help to check script for


Recommended Posts

hi guys,

appreciate if someone could take a look and suggest if there is any way to reduce memory leaks (if any), and to reduce memory and cpu usage.

and does this script pass to be run as a service? thanks.

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

If Not FileExists(@ScriptDir & "\settings.ini") Then
    $Set = FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    If $Set = 0 Then MsgBox(0, "Error", "Unable to copy default settings.ini")
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    $NP = Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    If $NP = 0 Then MsgBox(0, "Error", "Unable to open settings.ini file for modification.")
    Exit
EndIf

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

$save = DirCreate(IniRead($inifile, "config", "sendto", "sendto"))
If $save = 0 Then
    MsgBox(0, "Error", "Unable to create SENDTO folder. Please check settings.ini")
    Exit
EndIf

While 1
    If IniRead($inifile, "config", "date", "date") <> StringMid(@YEAR & @MON & @MDAY, 3) Then
        $sFileLocationName = StringMid(@YEAR & @MON & @MDAY, 3) & ".msg"
        $ID = IniWrite($inifile, "config", "date", StringMid(@YEAR & @MON & @MDAY, 3))
        If $ID <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new date to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
        $IL = IniWrite($inifile, "config", "LastEOF", "0")
        If $IL <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new LastEOF to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
    EndIf
    
    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()
    Dim $array
    $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", ''))
    $ll = False

    If FileOpen($sFileLocationName, 0) = -1 Then
        $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to access MSG file on " & _Now() & @CRLF)
        If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
    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
                    Exit
                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
    FileClose($sFileLocationName)
    AppendText($array)
    IniWrite($inifile, "config", "LastEOF", $nLastEOF)
EndFunc  ;==>MonitorChanges

Func AppendText($aT)
    For $x = 0 To UBound($aT) - 1
        $ans = StringRegExp($aT[$x], ".*" & (IniRead($inifile, "config", "search", "search")) & ".*", 3)
        For $r = 0 To UBound($ans) - 1
            $data = StringSplit($ans[$r], @CRLF, 1)
            For $i = 1 To $data[0]
                $Line = $data[$i]
                $array = StringSplit($Line, @TAB)
                If $array[0] > 11 And StringInStr($array[12], (IniRead($inifile, "config", "search", "search"))) Then
                    $found = "Yes"
                    $3 = StringTrimLeft($array[3], 1)
                    $13 = StringTrimRight($array[13], 2)
                    $archive = (IniRead($inifile, "config", "sendto", "sendto") & StringMid(@YEAR & @MON & @MDAY, 3) & "\")
                    If Not FileExists($archive) Then
                        $Dir = DirCreate($archive)
                        If $Dir = 0 Then
                            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to create SENDTO folder on " & _Now() & @CRLF)
                            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    Else
                        $FWL = FileWrite($archive & $3 & ".txt", $array[8] & @TAB & $13 & @CRLF)
                        If $FWL = 0 Then
                            $FW2 = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write ARCHIVES to settings.ini on " & _Now() & @CRLF)
                            If $FW2 = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    EndIf
                EndIf
            Next
        Next
    Next
    $Time = _DateAdd('n', (IniRead($inifile, "config", "delay", "delay")), _NowCalc())
    TraySetToolTip("CCG: F8 to exit. Next search on " & $Time)
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
Edited by iceberg

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

Link to comment
Share on other sites

hi guys,

appreciate if someone could take a look and suggest if there is any way to reduce memory leaks (if any), and to reduce memory and cpu usage.

and does this script pass to be run as a service? thanks.

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

If Not FileExists(@ScriptDir & "\settings.ini") Then
    $Set = FileInstall("set.ini", @ScriptDir & "\settings.ini", 1)
    If $Set = 0 Then MsgBox(0, "Error", "Unable to copy default settings.ini")
    MsgBox(0, "Missing configuration file restored", "Please update the default settings.ini before proceeding.")
    $NP = Run("notepad.exe " & @ScriptDir & "\settings.ini", "", @SW_MAXIMIZE)
    If $NP = 0 Then MsgBox(0, "Error", "Unable to open settings.ini file for modification.")
    Exit
EndIf

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

$save = DirCreate(IniRead($inifile, "config", "sendto", "sendto"))
If $save = 0 Then
    MsgBox(0, "Error", "Unable to create SENDTO folder. Please check settings.ini")
    Exit
EndIf

While 1
    If IniRead($inifile, "config", "date", "date") <> StringMid(@YEAR & @MON & @MDAY, 3) Then
        $sFileLocationName = StringMid(@YEAR & @MON & @MDAY, 3) & ".msg"
        $ID = IniWrite($inifile, "config", "date", StringMid(@YEAR & @MON & @MDAY, 3))
        If $ID <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new date to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
        $IL = IniWrite($inifile, "config", "LastEOF", "0")
        If $IL <> 1 Then
            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write new LastEOF to settings.ini on " & _Now() & @CRLF)
            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
            Exit
        EndIf
    EndIf
    
    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()
    Dim $array
    $nLastEOF = Number(IniRead($inifile, "config", "LastEOF", ''))
    $ll = False

    If FileOpen($sFileLocationName, 0) = -1 Then
        $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to access MSG file on " & _Now() & @CRLF)
        If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
    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
                    Exit
                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
    FileClose($sFileLocationName)
    AppendText($array)
    IniWrite($inifile, "config", "LastEOF", $nLastEOF)
EndFunc ;==>MonitorChanges

Func AppendText($aT)
    For $x = 0 To UBound($aT) - 1
        $ans = StringRegExp($aT[$x], ".*" & (IniRead($inifile, "config", "search", "search")) & ".*", 3)
        For $r = 0 To UBound($ans) - 1
            $data = StringSplit($ans[$r], @CRLF, 1)
            For $i = 1 To $data[0]
                $Line = $data[$i]
                $array = StringSplit($Line, @TAB)
                If $array[0] > 11 And StringInStr($array[12], (IniRead($inifile, "config", "search", "search"))) Then
                    $found = "Yes"
                    $3 = StringTrimLeft($array[3], 1)
                    $13 = StringTrimRight($array[13], 2)
                    $archive = (IniRead($inifile, "config", "sendto", "sendto") & StringMid(@YEAR & @MON & @MDAY, 3) & "\")
                    If Not FileExists($archive) Then
                        $Dir = DirCreate($archive)
                        If $Dir = 0 Then
                            $FW = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to create SENDTO folder on " & _Now() & @CRLF)
                            If $FW = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    Else
                        $FWL = FileWrite($archive & $3 & ".txt", $array[8] & @TAB & $13 & @CRLF)
                        If $FWL = 0 Then
                            $FW2 = FileWrite(IniRead($inifile, "config", "sendto", "sendto") & "error.log", "Unable to write ARCHIVES to settings.ini on " & _Now() & @CRLF)
                            If $FW2 = 0 Then MsgBox(0, "Error", "Unable to write to error.log", 10)
                        EndIf
                    EndIf
                EndIf
            Next
        Next
    Next
    $Time = _DateAdd('n', (IniRead($inifile, "config", "delay", "delay")), _NowCalc())
    TraySetToolTip("CCG: F8 to exit. Next search on " & $Time)
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
Try here: http://www.autoitscript.com/forum/index.ph...l=_ReduceMemory

Hope this helps!

When the words fail... music speaks.

Link to comment
Share on other sites

ok thanks.

Func _ReduceMemory($i_PID = -1)
    
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc;==> _ReduceMemory()

to which part of my script do I insert the above UDF?

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

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...