Jump to content

Auto script backup


Alek
 Share

Recommended Posts

Ok, new problem.

Memory leak :S

Can anyone see the leak??

#include <GUIConstants.au3>
Opt("TrayMenuMode",1)
AdlibEnable("Userinput",50)

;The program tittle.
Dim $Tittle = "Lolz"

;This is where the program will look for files to back up.
Dim $Dir2Backup = RegRead("HKEY_CURRENT_USER\SOFTWARE\Autoitbackup", "dir2backup")

;This is where to store the backed up files.
Dim $Backup2Dir = RegRead("HKEY_CURRENT_USER\SOFTWARE\Autoitbackup", "backup2dir")

;This is need to be decleared.
Dim $S_Paths = ""
Dim $Old_List_Sel = ""

;Create a way to exit the script.
$TrayItem_Exit = TrayCreateItem("Exit")

;Creat the GUI
$Form1 = GUICreate($Tittle, 400, 280)

$Label1 = GUICtrlCreateLabel("Directory to backup", 5, 5, 140, 20, $SS_SUNKEN)
$Label2 = GUICtrlCreateLabel("Directory to place Backups", 5, 30, 140, 20, $SS_SUNKEN)
$Label3 = GUICtrlCreateLabel("backed up files", 5, 65, 135, 20, $SS_SUNKEN+$SS_CENTER)

$Input2 = GUICtrlCreateInput($Backup2Dir, 150, 30, 215, 20,$ES_READONLY)
$Input1 = GUICtrlCreateInput($Dir2Backup, 150, 5, 217, 21,$ES_READONLY)

$Button1 = GUICtrlCreateButton("...", 370, 5, 25, 20)
$Button2 = GUICtrlCreateButton("...", 370, 30, 25, 20)

$List1 = GUICtrlCreateList("", 5, 85, 135, 188)
$ListView1 = GUICtrlCreateListView("Date|Time|Size", 145, 85, 250, 188)

GUISetState(@SW_SHOW)

While 1
    ;If its the first time checking or the last round was done.
    If $S_Paths = "" Then
        
        ;Reset the file checking.
        $S_Paths = $Dir2Backup
    EndIf
    
    ;Set the file paths to search in
    $S_File_Path = StringSplit($S_Paths,"|")
    
    ;Set $S_Paths to 1
    $S_Paths = ""
    
    ;ente a For loop to search in the diffrent filepaths
    For $x = 1 To $S_File_Path[0]
        
        ;see if the filepath is other then nothing.
        ;Could use:
        ;If Fileexists($S_FilePath[$x]) then
        ;but i think the way it is atm is faster.
        If $S_File_Path[$x] <> "" Then
            
            ;Find the first file.
            $S_First_File = FileFindFirstFile($S_File_Path[$x] & "\*.*")
            
            ;Enter a loop to get all the files in the current file path
            While 1
                ;Find the next File.
                $S_Files = FileFindNextFile($S_First_File)
                $S_File_Time = FileGetTime($S_File_Path[$x] & "\" & $S_Files)
                ;If @error then we have found all the files or there arent any here.
                If @error Then ExitLoop
                
                ;Check and see if its a file or a folder.
                If StringInStr(FileGetAttrib($S_File_Path[$x] & "\" & $S_Files), "D") Then
                    
                    ;Else added the foler's Path into the path list.
                    $S_Paths &= $S_File_Path[$x] & "\" & $S_Files & "|"
                    
                ElseIf StringRight($S_Files,3) = "au3" Then
                    
                    ;Check if the file has changed.
                    If $S_File_Time[0] & $S_File_Time[1] & $S_File_Time[2] & $S_File_Time[3] & $S_File_Time[4] & $S_File_Time[5] <> IniRead(@ScriptDir & "\Save.dat","Files",$S_File_Path[$x] & "\" & $S_Files,"0") Then
                    
                        ;Get the new dir to save the file as a backup
                        $Backup_Name = StringReplace($S_File_Path[$x] & "\" & $S_Files,$Dir2Backup,$Backup2Dir)
                        
                        ;Creat the new name with timestamp as part of its name.
                        $Backup_Name = StringReplace($Backup_Name,".au3",_Dateandtime() & ".au3")
                        
                        ;Write to console for debuging.
                        ConsoleWrite($Backup_Name & @CRLF)
                        
                        ;Copy and creat the dir in the backup system.
                        FileCopy($S_File_Path[$x] & "\" & $S_Files,$Backup_Name,9)
                        
                        ;Write the time of the file to a ini file.
                        IniWrite(@ScriptDir & "\Save.dat","Files",$S_File_Path[$x] & "\" & $S_Files,$S_File_Time[0] & $S_File_Time[1] & $S_File_Time[2] & $S_File_Time[3] & $S_File_Time[4] & $S_File_Time[5])
                        
                        ;Add the file to the list.
                        GUICtrlSetData($List1,StringTrimRight($S_Files,4))
                    EndIf
                EndIf
                Sleep(1)
            WEnd
        EndIf
    Sleep(1)
    Next
WEnd

Func _Dateandtime()
    Return @HOUR & ";" & @MIN & " " & @MDAY & "-" & @MON & "-" & @YEAR
EndFunc
 
 Func Userinput()
    ;the GUI msg stuff
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE)
        Case $Button1
            $File = FileSelectFolder("Select Dir to backup","")
            If $File <> "" Then
                GUICtrlSetData($Input1,$File)
                RegWrite("HKEY_CURRENT_USER\SOFTWARE\Autoitbackup", "dir2backup", "REG_SZ", $File)
                $Dir2Backup = $File
            EndIf
        Case $Button2
            $File = FileSelectFolder("Select Dir to backup","")
            If $File <> "" Then
                GUICtrlSetData($Input2,$File)
                RegWrite("HKEY_CURRENT_USER\SOFTWARE\Autoitbackup", "backup2dir", "REG_SZ", $File)
                $Backup2Dir = $File
            EndIf
    EndSwitch
    
    ;the tray message stuff.
    $tMsg = TrayGetMsg()
    Switch $tMsg
        Case $TrayItem_Exit
            Exit
        Case $TRAY_EVENT_PRIMARYDOUBLE
            GUISetState(@SW_SHOW)
    EndSwitch
    Sleep(10)
 EndFunc
Edited by Alek

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

im making a script that will auto backup my script every time i save the scripts.

the problem im having is that some scripts gets seen as something has changed in it even if they havent changed and i cant seem to find the problem.

the scripts that this happens to are always the same with but i cant really find anything in common with them :s

$foo = Run(@TempDir & "\test.bat", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While ProcessExists($foo)

your batch file will finish before the AU3 script has a chance to check for the PID, so you don't get STDOUT for the compare command.

You should change the logic of your script.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

$foo = Run(@TempDir & "\test.bat", "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While ProcessExists($foo)

your batch file will finish before the AU3 script has a chance to check for the PID, so you don't get STDOUT for the compare command.

You should change the logic of your script.

Cheers

Kurt

hmm, ok, thanks, do you have any other idea on how to compare files??

[font="Impact"]Never fear, I is here.[/font]

Link to comment
Share on other sites

yea, create a filecompare function. :D

maybe i can help you jumpstart one

func _CompareFiles($fX, $fY)
    if (fileexists($fX)=0) or (fileexists($fY)=0)  then 
        msgbox(0,"Error","one (or more) of the two files specified does not exists"& @lf& _
                            "You may have misspelled one of the files names, check your code")
        Exit
    EndIf
    $aTime_X = FileGetTime($fX)
    $aTime_Y = FileGetTime($fY)
    $iSize_X = FileGetSize($fX)
    $iSize_Y = FileGetSize($fY)
    
endfunc

So now you have the timestamps of both flies and the size of both files. that should give you a good start for comparison. If you want more in depth comparison like a difference between the two files i suggest creating a new function for that.

sounds like fun though, let me know what you come up with.

{= D

edit: added text and removed include statement.

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

i used the filegettime and check if the filetime has changed, use a ini to store the old time.

now i have a memory leak and i cant find it.

New script at top.

[font="Impact"]Never fear, I is here.[/font]

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