Jump to content

Greenseed
 Share

Recommended Posts

Include this script to your actual project and when SUP_Check() is called your script is going to download the next version and self update him self then restart.

I am sorry but i did not bother to make a UDF or a more user friendly and flexible module since i still have a lot to go on my initial project ;)

1- inside your project you include thoses line , all together and before your script actual code, beside your log system if you have one

#include "self_update.au3"
SUP_SetLogCallBack("_log") ; optional , used to send log to your allready log system if success self loggin is disable
SUP_SetCheckCallBack("SUP_DownloadProgress") ; optional , will send the % of the download in progress
SUP_Start("VALID HTTP URL To YPOUR NEW FILE","DOWNLOAD FULL FILE PATH NAME") ; mandatory

2- Now anywhere in your script you call this line to check for a newer version

$fileVersionUrl: is if your using a custom web page to send fast information on the file like version that optional, if empty will always update when check

$parentGui="": the self updated will create a gui with a progress bar and try to attach it self to the $parentGui , that optional

SUP_Check($fileVersionUrl="",$parentGui="")

3- to check if your script has just restarted after a update

if IsDeclared("updateComplete") then
ConsoleWrite("Update Complete Sucess Bravo!")
endif

File: SUP_SelfUpdate.au3

The script can be compiled alone for debug purpose , look for 1 line to uncomment inside the script if running alone

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=e:SelfUpdate.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include-once

;*************** Line to be use inside your own project************
;
;~ #include "self_update.au3"
;~ SUP_SetLogCallBack("_log")
;~ SUP_SetCheckCallBack("SUP_DownloadProgress")
;~ SUP_Start("e:SelfUpdate_FF.exe",@TempDir&"SelfUpdate_FF.exe")
;
;*******************************************************************


Global $SUP_LOGS_CALL[1] = [0]
Global $SUP_DOWN_CALL[1] = [0]
Global $SUP_URL
Global $SUP_FILE
Global $SUP_INFO_FILE
Global $SUP_DEST_FILE
Global $SUP_PID

;**************Uncomment to self debug that script alone*************
;~ SUP_Start(@ScriptDir&"SelfUpdate.exe",@TempDir&"SelfUpdate.exe")
;********************************************************************

Func SUP_Start($SUPURL,$SUPFILE)
$SUP_URL = $SUPURL
$SUP_FILE = $SUPFILE
$SUP_INFO_FILE = $SUP_FILE&".info"
if $CmdLine[0] > 0 Then
Switch $CmdLine[1]
case "update"
SUP_InitInfo()
if Not ProcessWaitClose($SUP_PID,30) Then
SUP_Log("UPDATE - Could not complete since """&$SUP_DEST_FILE&""" will not close",false)
Exit
EndIf
if Not FileCopy($SUP_FILE,$SUP_DEST_FILE,1) Then
SUP_Log("UPDATE - Could not copy the new file to """&$SUP_DEST_FILE&"""",false)
Exit
EndIf
Run($SUP_DEST_FILE&" updatecomplete")
exit
case "updatecomplete"
SUP_Delete()
;~ SUP_Log("UPDATE - Complete sucess")
Assign("updateComplete",true,2)
EndSwitch
;~ Else
;~ SUP_Check() ;for testing to be remove when linked with main
EndIf
;~ sleep(5000)
EndFunc

Func SUP_InitInfo()
local $data = FileRead($SUP_INFO_FILE)
$data = StringSplit($data,"|")
if $data[0] <> 2 Or $data[1]="" or $data[2] = "" Then
SUP_Log("UPDATE - Info File is invalid")
sleep(4000)
exit
EndIf
$SUP_PID = $data[1]
$SUP_DEST_FILE = $data[2]
EndFunc
Func SUP_Delete()
FileDelete($SUP_FILE)
FileDelete($SUP_INFO_FILE)
EndFunc

Func SUP_SetCheckCallBack($funcCallBack)
ReDim $SUP_DOWN_CALL[$SUP_DOWN_CALL[0]+2]
$SUP_DOWN_CALL[0] += 1
$SUP_DOWN_CALL[$SUP_DOWN_CALL[0]] = $funcCallBack
EndFunc
Func SUP_Check($fileVersionUrl="",$parentGui="")
;~ if $RUN then return
local $version = FileGetVersion(@AutoItExe)
local $nextVersion = "always update"
if $fileVersionUrl <> "" Then $nextVersion = BinaryToString(InetRead($fileVersionUrl,1))
;~ SUP_Log($version&" "&$nextVersion)
if $nextVersion = "" Or $version = $nextVersion Then
SUP_Log("UPDATE - No new version found")
Return
EndIf
SUP_Log("UPDATE - New version found")
SUP_Log("UPDATE - Downloading version "&$nextVersion)
local $size = InetGetSize($SUP_URL,1)
FileDelete($SUP_FILE)
local $handle = InetGet($SUP_URL,$SUP_FILE,1,1)
local $byteRead, $error = false
#include <WindowsConstants.au3>
GUICreate("ProgressBar",200,30,-1,-1,BitOR($WS_MINIMIZEBOX,$WS_DLGFRAME,$WS_POPUP,$WS_GROUP,$WS_CLIPSIBLINGS), BitOR($WS_EX_TOOLWINDOW,$WS_EX_TOPMOST),$parentGui)
local $progress = GUICtrlCreateProgress(0,0,200,30)
GUISetState(@SW_SHOW)
local $pct
do
Sleep(250)
$byteRead = InetGetInfo($handle, 0)
if @error Then
$error = true
ExitLoop
EndIf
GUICtrlSetData($progress,$byteRead*100/$size)
$pct = Round($byteRead*100/$size,2)
SUP_Log("UPDATE - Percent done: "&$pct)
If $SUP_DOWN_CALL[0] > 0 Then
For $i = 1 to $SUP_DOWN_CALL[0]
Call($SUP_DOWN_CALL[$i],$pct)
Next
EndIf
Until InetGetInfo($handle, 2)
GUIDelete()
if $error or Not InetGetInfo($handle,3) Then
InetClose($handle)
SUP_Log("UPDATE - Downloading next version failed")
SUP_Delete()
Return
EndIf
InetClose($handle)
FileDelete($SUP_INFO_FILE)
FileWrite($SUP_INFO_FILE,@AutoItPID&"|"&@ScriptFullPath)
SUP_Log("UPDATE - Exiting to complete update")

Run($SUP_FILE&" update")
if @error Then
SUP_Log("UPDATE - Unable to open the new file")
SUP_Delete()
Return
endif
SUP_Log("UPDATE - Exiting")
sleep(1000)
Exit
EndFunc

Func SUP_SetLogCallBack($funcCallBack)
ReDim $SUP_LOGS_CALL[$SUP_LOGS_CALL[0]+2]
$SUP_LOGS_CALL[0] += 1
$SUP_LOGS_CALL[$SUP_LOGS_CALL[0]] = $funcCallBack
EndFunc
Func SUP_Log($msg,$useCallBack=true)
If $useCallBack And $SUP_LOGS_CALL[0] > 0 Then
For $i = 1 to $SUP_LOGS_CALL[0]
Call($SUP_LOGS_CALL[$i],$msg)
Next
Return
EndIf
if @Compiled Then MsgBox(0,"Message",$msg)
ConsoleWrite($msg&@CRLF)
EndFunc
Edited by Greenseed

GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Link to comment
Share on other sites

  • 4 years later...

Did somebody ever got this working? Tried like posted above, a small progressbar appears for some miliseconds - but the script is not updated. Or any other alternatives? The Compiled Exe should update itself without asking if a new version is available (wether via web or path, but prefered via filepath)

Edited by AIstarter
Link to comment
Share on other sites

4 hours ago, AIstarter said:

Did somebody ever got this working? Tried like posted above, a small progress appears for some milliseconds - but the script is not updated. Or any other alternatives? The Compiled Exe should update itself without asking if a new version is available (whether via web or path, but preferred via file path)

Here is a simple function I use to update my application loader. It looks for a file at 2 locations, if one is unavailable it uses a backup. Then creates a batch file, downloads the new file with a different name, loads the batch, ends the process of the original file, renames and reloads. All pretty seamless. Doesn't offer a progress bar, but happens so fast you wouldn't see it anyway. I'm sure it needs clean up and consolidation, but it works, so I've left it as is.

 

Func Self_Update()
    If Not IsDeclared("update_path") Then
        Global $Path1 = FileGetShortName("\\network\path\to\updated\exe")
        Global $Path2 = FileGetShortName("c:\alernate\location\for\update\if\network\unavailable")
        If Not FileExists($Path1 & "\Application loader.exe") Then
            If Not FileExists($Path2 & "\Application loader.exe") Then
                MsgBox(48, "Update Error!", "Unable to locate an update file!")
            Else
                $Update_Path = $Path2
            EndIf
        Else
            $Update_Path = $Path1
        EndIf
        $NewVersion = FileGetVersion($Update_Path & "\Application loader.exe")
        $OldVersion = FileGetVersion(@ScriptFullPath)
        $Results = _StringCompareVersions($OldVersion, $NewVersion)
        If $Results = "-1" Then
            SplashTextOn("Updating Loader", "Please wait... This may take a few moments.", "400", "100", "-1", "-1", 50, "", "", "")
            Sleep(1000)
            FileCopy($Update_Path & "\Application loader.exe", @ScriptFullPath & ".new")
            Local $BatchPath = @ScriptDir & '\update.bat'
            $FileData = "@echo off" & @CRLF & _
                    "ping localhost -n 2 > nul" & @CRLF & _
                    ":loop" & @CRLF & _
                    'del /Q "' & @ScriptFullPath & '"' & @CRLF & _
                    'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF & _
                    'move "' & @ScriptFullPath & '.new" "' & @ScriptFullPath & '"' & @CRLF & _
                    'start /B "Loading" "' & @ScriptFullPath & '"' & @CRLF & _
                    'del /Q "' & $BatchPath & '"' & @CRLF & _
                    "exit"
            FileWrite($BatchPath, $FileData)
            FileSetAttrib($BatchPath, "+H")
            Run($BatchPath, "", @SW_HIDE)
            SplashOff()
            Write2Log("Updated Application to version " & $NewVersion) ; - write to log file, remove if you don't need this.
            Exit
        EndIf
    EndIf
EndFunc   ;==>Self_Update

You will also need the attached UDF by @PeteF to make the version compare I use work.

StringCompareVersions.au3

Edited by zone97

 

Spoiler

WinSizer 2.1 (01/04/2017) - Download - [ Windows Layout Manager ]
Folder+Program (12/23/2016) - Download - [ USB Shortcut Creator ]

 

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

×
×
  • Create New...