Sign in to follow this  
Followers 0
rony2006

Loop problem

3 posts in this topic

#1 ·  Posted (edited)

Hello,

At work we have a computer that inspect every product for detecting bad mounting.

I made made a script that back-up important files each day, on a USB stick because if the HDD fails, to have the important files on a external device.

The code for back-up is this:

#include <MsgBoxConstants.au3>



backup()

Func backup()


FileDelete ("D:\SIRIUS\Applications\Coake\Images\Fehlerbilder\Kamera1\*.bmp")
FileDelete ("D:\SIRIUS\Applications\Coake\Images\Fehlerbilder\Kamera2\*.bmp")
Sleep (60000)
RunWait ("C:\Documents and Settings\Administrator\Desktop\backup.bat")

    $file = FileOpen("G:\log.txt", 1)

; Check if file opened for writing OK
If $file = -1 Then
    Exit
EndIf

FileWriteLine($file, "Data ultimului back-up:  " & @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & @CRLF)

FileClose($file)

Sleep (900000)
call ("backup")


Endfunc

 

Also I made another script to prevent the steal of the USB stick:

#include <MsgBoxConstants.au3>




Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = "G:\check.txt"

    Local $iFileExists = FileExists($sFilePath)
    
Sleep (60000)
    ; Display a message of whether the file exists or not.
    If $iFileExists Then


    Else
        ProcessClose ( "Coake.exe" )
        MsgBox(48, "ATENTIE!!!", "Stick-ul USB destinat pentru backup a fost inlaturat!" & @CRLF & "Introduce-ti stick-ul in calculator iar apoi chemati un electrician/inginer de proces!" & @CRLF & "Dupa introducerea stick-ului, programul Coake poate fi pornit din nou!")


    EndIf

The problem is that after a period of time, I get the error from atachment.

Is another way to make the loop? At the back-up script I used While 1 and Wend and is working but at "check USB stick" it doesn't working or I don't know for sure were to put "while 1" and "wend".

Can you help me please?

P.S. I use Windows 2000 and a old version of autoit.

oiuo.jpg

Edited by rony2006

Share this post


Link to post
Share on other sites



You call the function "backup" fro within the function "backup". That's a function calling itself, and that's called recursion. You can only do recursion a number of levels deep. But since you have a really long waiting time (900 sec), you don't notice it immediately. Here's a demonstration:

$iLevel = 0

myRecursiveFunction()

Exit

Func myRecursiveFunction()
    $iLevel += 1
    ConsoleWrite($iLevel & @CRLF)
    myRecursiveFunction()
EndFunc   ;==>myRecursiveFunction

This will run for a certain number of levels, then reproduce that error you have there.

The simplest fix would be to make the function non-recursive, in your case, something like this:

#include <MsgBoxConstants.au3>

While True
    backup()
    Sleep(900000)
WEnd

Func backup()


    FileDelete("D:\SIRIUS\Applications\Coake\Images\Fehlerbilder\Kamera1\*.bmp")
    FileDelete("D:\SIRIUS\Applications\Coake\Images\Fehlerbilder\Kamera2\*.bmp")
    Sleep(60000)
    RunWait("C:\Documents and Settings\Administrator\Desktop\backup.bat")

    $file = FileOpen("G:\log.txt", 1)

    ; Check if file opened for writing OK
    If $file = -1 Then
        Exit
    EndIf

    FileWriteLine($file, "Data ultimului back-up:  " & @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & @CRLF)

    FileClose($file)

EndFunc   ;==>backup

It will do pretty much the same thing, only the long wait and the new function call have been moved out of the function (so it's not recursive any more).


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Thank you for the quick answer.

I tested now on the computer and the loop is working ok.

Have a nice day!

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