Jump to content

Code optimization


Baraoic
 Share

Recommended Posts

I have a big script I use for some daily tasks that have to be run and know there has got to be a better way to code this I just cant think of it. Was wondering if anybody would be able to point me in the right direction? Here's what I have

$01Dir = "\\01\c$\logs\"
$02Dir = "\\02\c$\logs\"
$Search = FileFindFirstFile($01Dir & "*.rtl")
If $Search = -1 Then
    Exit
EndIf
If Int(@MDAY) < 11 Then
    $Yesterday = 0 & Int(@MDAY - 1)
Else
    $Yesterday = @MDAY - 1
EndIf
While 1
    $File = FileFindNextFile($Search)
    If @error Then ExitLoop
    $aFile = FileGetTime($01Dir & $File)
    If $aFile[1] = @MON Then
        If $aFile[2] = @MDAY Then
            _ArrayAdd($aToday, FileGetTime($01Dir & $File, 0, 1) & "!" & $01Dir & "!" & $File)
        ElseIf $aFile[2] = $Yesterday Then
            _ArrayAdd($aYesterday, FileGetTime($01Dir & $File, 0, 1) & "!" & $01Dir & "!" & $File)
        EndIf
    EndIf
WEnd
FileClose($Search)
$Search2 = FileFindFirstFile($02Dir & "*.rtl")
If $Search2 = -1 Then
    Exit
EndIf
While 1
    $File = FileFindNextFile($Search2)
    If @error Then ExitLoop
    $aFile = FileGetTime($02Dir & $File)
    If $aFile[1] = @MON Then
        If $aFile[2] = @MDAY Then
            _ArrayAdd($aToday, FileGetTime($02Dir & $File, 0, 1) & "!" & $02Dir & "!" & $File)
        ElseIf $aFile[2] = $Yesterday Then
            _ArrayAdd($aYesterday, FileGetTime($02Dir & $File, 0, 1) & "!" & $02Dir & "!" & $File)
        EndIf
    EndIf
WEnd
FileClose($Search2)
_ArraySort($aToday, 1)
_ArraySort($aYesterday, 1)
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "03E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $03 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $03[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $03eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "03E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $03 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $03[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "04E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $04 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $04[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $04eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "04E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $04 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $04[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "05E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $05 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $05[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $05eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "05E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $05 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $05[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "06E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $06 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $06[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $06eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "06E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $06 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $06[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "07E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $07 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $07[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $07eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "07E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $07 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $07[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
For $x = 1 To UBound($aToday) - 1
    $s = StringSplit($aToday[$x], "!")
    If StringInStr(FileReadLine($s[2] & $s[3], 11), "08E") > 0 Then
        If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
            $08 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
            ExitLoop
        ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
            $08[0] = "Red"
            ExitLoop
        EndIf
    EndIf
Next
If $08eb[0] = "Error" Then
    For $x = 1 To UBound($aYesterday) - 1
        $s = StringSplit($aYesterday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), "08E") > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $08 = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $08[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
EndIf
_ExcelWriteCell($oExcel, $03[0], 7, 3)
_ExcelWriteCell($oExcel, $04[0], 8, 3)
_ExcelWriteCell($oExcel, $05[0], 9, 3)
_ExcelWriteCell($oExcel, $06[0], 10, 3)
_ExcelWriteCell($oExcel, $07[0], 11, 3)
_ExcelWriteCell($oExcel, $08[0], 12, 3)
_ExcelBookSave($oExcel)

it's looking for the last two days worth of logs for our BEX of servers. The reason I did it like that because sometimes a server will be backed up more then once in a day and I wanted it to find the most recent backup to see if it failed or completed. Any help would be nice and thanks. I declared all the arrays in the beginning with 'error' so that's why it has if $03[0] = "error" also here's a copy of the log files its looking at with a few things xxx'ed out

Sat Dec 20 05:00:00 2008 SNBGEN1110I LogFile(C:\logs\00001118.rtl) opened by Module(syncrtm)

Sat Dec 20 05:00:00 2008 SNBGEN1111I Module Build ID:[arch(winnt) ver(3.0) build_time(02:17:31) build_date(Sep 1 2007)]

Sat Dec 20 05:00:00 2008 SNBGEN1113I Process Info:[pid(4376) user(SYSTEM) cmdline(xxx)]

Sat Dec 20 05:00:00 2008 SNBGEN1112I Env:SSPRODIR(xxx)

Sat Dec 20 05:00:00 2008 SNBGEN1112I Env:SSICMAPI(-lcl no -sbs 65535 -tnd -a 6123)

Sat Dec 20 05:00:00 2008 SNBGEN3066D ms_register_module_str: listening on TCP port xxx

Sat Dec 20 05:00:00 2008 SNBRTM1004I Processing message execute_task_new on socket 1

Sat Dec 20 05:00:00 2008 SNBRTM5692I Connecting to sssvh at xxx ........

Sat Dec 20 05:00:01 2008 SNBRTM5768I Task 07E (1229738400) connected to snapvault job handler successfully

Sat Dec 20 05:00:01 2008 SNBRTM3066D SVH socket(3)

Sat Dec 20 05:00:01 2008 SNBRTM5662J Starting task 07E (1229738400)

Sat Dec 20 05:15:59 2008 SNBRTM1004I Processing message job_completed on socket 3

Sat Dec 20 05:15:59 2008 SNBRTM5654I Job status: Completed(0), job flags: (0), task flags: (0), id: 1229738400

Sat Dec 20 05:15:59 2008 SNBRTM5673I Connection closed on socket 1 (READZERO)

Sat Dec 20 05:18:59 2008 SNBRTM1279I RTM timed out, going down now

Link to comment
Share on other sites

First of all use Func() for repetitive tasks:

#include <Array.au3>

Global $aToday, $aYesterday
Global $03, $04, $05, $06, $07, $08

If Int(@MDAY) < 11 Then
    $Yesterday = 0 & Int(@MDAY - 1)
Else
    $Yesterday = @MDAY - 1
EndIf

LoadData("\\01\c$\logs\")
LoadData("\\02\c$\logs\")

_ArraySort($aToday, 1)
_ArraySort($aYesterday, 1)

ParseData($03, "03E")
ParseData($04, "04E")
ParseData($05, "05E")
ParseData($06, "06E")
ParseData($07, "07E")
ParseData($08, "08E")

_ExcelWriteCell($oExcel, $03[0], 7, 3)
_ExcelWriteCell($oExcel, $04[0], 8, 3)
_ExcelWriteCell($oExcel, $05[0], 9, 3)
_ExcelWriteCell($oExcel, $06[0], 10, 3)
_ExcelWriteCell($oExcel, $07[0], 11, 3)
_ExcelWriteCell($oExcel, $08[0], 12, 3)
_ExcelBookSave($oExcel)

Func LoadData($Dir)
    $Search = FileFindFirstFile($Dir & "*.rtl")
    If $Search = -1 Then
        Exit
    EndIf
    While 1
        $File = FileFindNextFile($Search)
        If @error Then ExitLoop
        $aFile = FileGetTime($Dir & $File)
        If $aFile[1] = @MON Then
            If $aFile[2] = @MDAY Then
                _ArrayAdd($aToday, FileGetTime($Dir & $File, 0, 1) & "!" & $Dir & "!" & $File)
            ElseIf $aFile[2] = $Yesterday Then
                _ArrayAdd($aYesterday, FileGetTime($Dir & $File, 0, 1) & "!" & $Dir & "!" & $File)
            EndIf
        EndIf
    WEnd
    FileClose($Search)
EndFunc

Func ParseData(ByRef $data, $value)
    For $x = 1 To UBound($aToday) - 1
        $s = StringSplit($aToday[$x], "!")
        If StringInStr(FileReadLine($s[2] & $s[3], 11), $value) > 0 Then
            If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                $data = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                ExitLoop
            ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                $data[0] = "Red"
                ExitLoop
            EndIf
        EndIf
    Next
    If $data[0] = "Error" Then
        For $x = 1 To UBound($aYesterday) - 1
            $s = StringSplit($aYesterday[$x], "!")
            If StringInStr(FileReadLine($s[2] & $s[3], 11), $value) > 0 Then
                If StringInStr(FileReadLine($s[2] & $s[3], 13), "Completed") > 0 Then
                    $data = StringRegExp(FileReadLine($s[2] & $s[3], 1), "\d{1,2}\s\d{1,2}\:\d{1,2}\:\d{1,2}", 1)
                    ExitLoop
                ElseIf StringInStr(FileReadLine($s[2] & $s[3], 13), "failure") > 0 Then
                    $data[0] = "Red"
                    ExitLoop
                EndIf
            EndIf
        Next
    EndIf
EndFunc
Link to comment
Share on other sites

I didn't even think of that thank you. I tend to do things the hard way, but that will make it a lot cleaner and more efficient. The problem is though I hate having it open the files so many times. I know using fileopen is best, but I only read a line or two for each file. Then there's the thing of it reading the files multiple times because once it finds if 03 was completed it then has to go through all the files again to fine 04.

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