DigDeep

Continue to write _FileWriteLog after system restart

4 posts in this topic

#1 ·  Posted

I have the below function set for log files inside my code using it at multiple locations.

I wanted to continue the log file after Step 6 when the computer restarts.

Local $log = 0

Func LogFilePath()
    If $log = 0 Then
        $GetLog = "C:\Temp\Test\Test.log"
        _FileCreate($GetLog)
          If FileExists($GetLog) Then
            ; Log Line 1
            ; Log Line 2
            ; Log Line 3
            ; Log line 4
        EndIf
        $log = 1
    EndIf
EndFunc

; Write some functions here...
; Step 1
LogFilePath()
_FileWriteLog($GetLog, "Line 5")
; Step 2
LogFilePath()
_FileWriteLog($GetLog, "Line 6")
; Reboot
; Want to continue the log lines after line 6.

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

You need to save the status of your script in a location that survives the reboot (Ini-file, registry ...).
First step in your script is then to check and reset this state. You then call your functions depending on the state.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

I you use FileOpen with mode = 1 then it will always append to end of your file something like:

#include <File.au3>

OnAutoItExitRegister("_LogClose")

Global $hLogFile, $sLogFile, $bLog = False
LogFilePath()

Func LogFilePath()
    If $bLog = False Then
        $sLogFile = EnvGet("SystemDrive") & "\Temp\Test\Test.log"
        $hLogFile = FileOpen($sLogFile, 9) ;~ Create Folder/File and append to end of file if it already exists
        If $hLogFile > -1 Then
            _FileWriteLog($sLogFile, "Line 1")
            _FileWriteLog($sLogFile, "Line 2")
            _FileWriteLog($sLogFile, "Line 3")
            _FileWriteLog($sLogFile, "Line 4")
            $bLog = True
        EndIf
    EndIf
EndFunc

; Step 1 Function
;~ Check if Step 1 has been completed
If FileExists(@ProgramFilesDir & "\Filepath1\Filename1.exe") = 0 Then
    ;~ Run Step 1 Installer here
    RunWait('MsiExec.exe /i "' & @ScriptDir & '\FileName1.msi" /qb /norestart')
    If $bLog = True Then _FileWriteLog($sLogFile, "Step 1 Completed")
EndIf

; Step 2 Function
If RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}", "DisplayName") = "" Then
    ;~ Run Step 2 Installer here
    RunWait('MsiExec.exe /i "' & @ScriptDir & '\FileName2.msi" /qb /norestart')
    If $bLog = True Then _FileWriteLog($sLogFile, "Step 2 Completed")
EndIf

; Reboot
; Step 3 Function
; Want to continue the log lines after line 6.

Func _LogClose()
    FileClose($hLogFile)
    Exit
EndFunc

 

1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted

Example:

#include <MsgBoxConstants.au3>

Global $iState = Number(IniRead(@ScriptDir & "\State.ini", "State", "State", 0))

; Step 1
If $iState < 1 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

; Step 2
If $iState < 2 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

; Step 3
If $iState < 3 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

; Step 4
If $iState < 4 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

; Step 5
If $iState < 5 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

; Step 6
If $iState < 6 Then
    $iState += 1
    MsgBox(0, "", "Executing Step " & $iState)
    IniWrite(@ScriptDir & "\State.ini", "State", "State", $iState)
    If MsgBox($MB_YESNO, "Step " & $iState, "Should I exit the script here?") = 6 Then Exit
EndIf

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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