Jump to content
silmarilwest

Update/autoupdate function(s)

Recommended Posts

silmarilwest

I'm definitely not the first to write something like this, but I have customized the code enough that I thought it might be useful.

Requires an http server in current form, although you could modify to use unc paths fairly easily.

#AutoIt3Wrapper_Res_Fileversion=1.0.0.1 ;for self checking file version

HotKeySet("{F2}", "UpdateCheck") ;call update using hotkey for simplicity in example

Global $updatescript = "updatescript.cmd" ;define batch update filename cmd or bat should work
Global $otherini = IniRead( "settings.ini", "Files", "otherini", "settings.ini") ;check for alternate ini definition
Global $autoupdate = IniRead( $otherini, "Files", "autoupdate", "False") ;check for auto update setting in ini
Global $versionurl = IniRead( $otherini, "Files", "versionurl", "http://yourserver/yourpath/version.txt") ;define url for version check
Global $latestexe = IniRead( $otherini, "Files", "latestexe", "http://yourserver/yourpath/latestexe.exe") ;define url for latest version

If $autoupdate = "True" Then
    UpdateCheck() ;check for updates at launch if ini autoupdate value is true
EndIf

Func UpdateCheck()
    $currentversion = FileGetVersion(@AutoItExe) ;check exe version
    $latestversion = BinaryToString(InetRead($versionurl,1)) ;get latest version
    If $currentversion = $latestversion Then
        If $autoupdate <> "True" Then
            MsgBox(64, "Update Check", "You are running the latest version: " & $currentversion)
            Return
        Else
            $autoupdate = "False" ;if autoupdate is true we want to suppress version confirmation
            Return
        EndIf
    ElseIf $latestversion <> "" Then ;you could change this to only update for greater versions, shouldn't matter if you maintin the version info properly
        Update()
    Else ;version file was either unavailable or blank
        If $autoupdate <> "True" Then
            MsgBox(64, "Update Check", "Could not contact update server")
            Return
        Else
            $autoupdate = "False" ;if autoupdate is true we want to suppress the error
            Return
        EndIf
    EndIf
EndFunc

Func Update()
    $updatedl = InetGet( $latestexe, "latest.exe", 1, 1) ;download update
    $updatesize = InetGetSize( $latestexe) ;get total size for progress bar
    ProgressOn("Update", "Downloading...", "0%")
    While Not InetGetInfo($updatedl, 2) ;loop and update progress bar until download is complete
        Sleep(100)
        $updaterec = InetGetInfo($updatedl, 0)
        $upct = Int($updaterec / $updatesize * 100)
        ProgressSet($upct, $upct & "%")
    WEnd
    ProgressOff()
    If InetGetInfo( $updatedl, 4) <> 0 Then ;check for download error
        If $autoupdate <> "True" Then
            MsgBox(64, "Update Check", "Unable to download or write file")
        Else
            $autoupdate = "False" ;again no error message if autoupdate is true
        EndIf
    Else ;success, write batch script
        $exename = @ScriptName
        $pid = @AutoItPID
        FileWriteLine( $updatescript, "@echo off")
        FileWriteLine( $updatescript, ":loop")
        FileWriteLine( $updatescript, "tasklist /fi " & '"pid eq ' & $pid & '" | find ":" > nul') ;batch file won't continue until old autoit exe process id terminates
        FileWriteLine( $updatescript, "if errorlevel 1 (")
        FileWriteLine( $updatescript, "  ping 127.0.0.1 -n 2")
        FileWriteLine( $updatescript, "  goto loop")
        FileWriteLine( $updatescript, ") else (")
        FileWriteLine( $updatescript, "  goto continue")
        FileWriteLine( $updatescript, ")")
        FileWriteLine( $updatescript, ":continue")
        FileWriteLine( $updatescript, "del " & '"' & $exename & '"') ;deletes old exe
        FileWriteLine( $updatescript, "ren latest.exe " & '"' & $exename & '"') ;renames new exe to the same name
        FileWriteLine( $updatescript, 'start "" ' & '"' & $exename & '"' & " " & $CmdLineRaw) ;launches with any parameters the old exe had
        FileWriteLine( $updatescript, "start /b """" cmd /c del ""%~f0""&exit /b") ;batch file self deletes
        Run( $updatescript, @ScriptDir, @SW_HIDE) ;launch batch file in hidden mode
        Exit ;exit so batch file can continue
    EndIf
EndFunc

Forgive my sloppy coding  >_<

Share this post


Link to post
Share on other sites
Celtic88

Goood thank you

Share this post


Link to post
Share on other sites
allcapone1912

great code

but i get an error with it when using via VirtualBox, any idea why?

if using direct from PC, works great

Share this post


Link to post
Share on other sites
JLogan3o13

@allcapone1912, you might notice that this thread is over 2 years old? Or that the OP has not been on the forum in two years? Those two items greatly lessen the chance of you getting your answer (which is why we discourage resurrecting old threads rather than starting new ones). You are much better off by starting a new thread in the General Help & Support forum, which will put many more eyes on the problem.

In addition, trying to get assistance and giving us no more than "it's broke" doesn't help us help you. You say you get an error - it would be beneficial if you actually share what that error is ;)

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
allcapone1912
1 minute ago, JLogan3o13 said:

@allcapone1912, you might notice that this thread is over 2 years old? Or that the OP has not been on the forum in two years? Those two items greatly lessen the chance of you getting your answer (which is why we discourage resurrecting old threads rather than starting new ones).

In addition, trying to get assistance and giving us no more than "it's broke" doesn't help us help you. You say you get an error - it would be beneficial if you actually share what that error is ;)

sorry for the confusion

in VirtualBox script just create the updatescript.cmd and latest.exe files and then just exit without running cmd file(batch file is not launched)
On main PC its work fine and i really dont get whats the problem with VM

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

×