Jump to content

Auto-update feature with periodic auto-checking


monter
 Share

Recommended Posts

Here is an example of auto-checking/auto-updating module.

Posted Image

After launching wait at least one minute few 4 seconds to let the script to check if exists newer version on server.

It's a simple and not perfect written script, I will upgrade it soon :idea: to make it better.

Edit 08.05.2010 (v. 0.20): fixed downloading link, in 0.10 update would failed... :)

Edit 09.05.2010 (v. 0.30): fixed "checking of previous checking time".

Edit 18.05.2010 (v. 0.40): improved variable AdlibRegister intervals. After launching - 4s, then 60s.

Edit 05.06.2010 (v. 0.50): Added icon, several fixes in UpdateCheck module, added update info MsgBox.

Now, after few 4 seconds after launching script checks if exists a new version on the server. If not, then checks every chosen period of time (24h or 60s)

Here is a link to older EXE file: AutoUpdater.exe (v. 0.20). After launching you can experience of auto-checking, auto-updating and auto-relauchning the newest version of compiled script with available source in it.

#NoTrayIcon
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=G:\util\AutoUpdater.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Comment=http://monter.FM
#AutoIt3Wrapper_Res_Description=Example of auto-update function in AutoIt3 script.
#AutoIt3Wrapper_Res_Fileversion=0.3.0.0
#AutoIt3Wrapper_Res_LegalCopyright=monter.FM
://////=__=..
://////=__=.
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Date.au3>
#include <Timers.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
$fnScript = 'Auto Updater (example script)' ;full script name
$pScript = StringSplit(@ScriptName, '.')
$pScript = $pScript[1]
$script = $pScript
If $script <> 'AutoUpdater' Then $script = 'AutoUpdater' ;proper script name (if user changed it)
Global $srvPthExe, $srvPthUpd, $version, $pos[3], $chkFrq, $msgShw, $rcntUpdChk, $rcntUsed
$ini = @ScriptDir & '\' & $script & '.ini'
FileChangeDir(@ScriptDir)
OnAutoItExitRegister('OnExit')
FileInfo()
$title = $fnScript & ' ' & $sVer
$dirMonter = @AppDataDir & '\monter.FM' ;directory for monter.FM's scripts
$filUpd = @TempDir & '\' & $script & '.upd' ;file checking update from the server
$filBat = @TempDir & '\' & $script & '.bat' ;temporary file for killing old exe and launching new version
$src = @TempDir & '\' & $script & '.au3' ;source file
If FileExists($filUpd) Then FileDelete($filUpd)
If FileExists($filBat) Then FileDelete($filBat)
$version = IniRead($ini, 'Main', 'version', $sVer)
$iniDel = 0
If $rcntUsed = '' Or ($CmdLine[0] > 0 And $CmdLine[1] = '-i') Then Install()
If ($CmdLine[0] > 0 And $CmdLine[1] = '-u') Then UpdateIcons()
If $sVer > $version Then UpdateIni()
IniCheck()
IniFRead()
Const $wi = 370
Const $he = 213
If $pos[1] > @DesktopWidth - $wi Then $pos[1] = @DesktopWidth - $wi - 50
If $pos[1] < 0 Then $pos[1] = 50
If $pos[2] > @DesktopHeight - $he Then $pos[2] = @DesktopHeight - $he - 100
If $pos[2] < 0 Then $pos[2] = 50
For $i = 1 To 2
    If $pos[$i] = 'default' Then $pos[$i] = -1
Next

#region ### START Koda GUI section ### Form=AutoUpdater.kxf
$frmMain = GUICreate($title, $wi, $he, $pos[1], $pos[2])
$mnuFil = GUICtrlCreateMenu('&File')
$mnuFilQuit = GUICtrlCreateMenuItem('&Quit' & @TAB & 'Ctrl+Q', $mnuFil)
GUICtrlCreateMenuItem('', $mnuFil)
$mnuFilUnins = GUICtrlCreateMenuItem('Uninstall', $mnuFil)
$mnuHlp = GUICtrlCreateMenu('&Help')
$mnuHlpAbt = GUICtrlCreateMenuItem('&About', $mnuHlp)
$lblExpl = GUICtrlCreateLabel('This is an example of auto-update function in AutoIt3 script.', 44, 4, 283, 17)
$grpChkFrq = GUICtrlCreateGroup('Update checking frequency', 4, 28, 261, 53)
$radChkFrq0 = GUICtrlCreateRadio('&every 24 hours (default)', 24, 44, 105, 33, BitOR($GUI_SS_DEFAULT_RADIO, $BS_MULTILINE))
GUICtrlSetTip(-1, 'that is 86400 seconds')
$radChkFrq1 = GUICtrlCreateRadio('e&very 60 seconds (testing purpose)', 144, 44, 105, 33, BitOR($GUI_SS_DEFAULT_RADIO, $BS_MULTILINE))
If $chkFrq > 86399 Then
    GUICtrlSetState($radChkFrq0, $GUI_CHECKED)
Else
    GUICtrlSetState($radChkFrq1, $GUI_CHECKED)
EndIf
GUICtrlCreateGroup('', -99, -99, 1, 1)
$cbxMsgShw = GUICtrlCreateCheckbox('show &ALL messages (including hidden)', 284, 30, 73, 53, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_MULTILINE))
If $msgShw = 1 Then
    GUICtrlSetState($cbxMsgShw, $GUI_CHECKED)
Else
    GUICtrlSetState($cbxMsgShw, $GUI_UNCHECKED)
EndIf
$grpSrvSet = GUICtrlCreateGroup('Server settings (usually hidden in .INI file)', 4, 88, 361, 73)
$lblPthExe = GUICtrlCreateLabel('Path to script file:', 16, 112, 85, 17)
$srvPthExe = InpPth($srvPthExe)
$inpPthExe = GUICtrlCreateInput('', 112, 108, 153, 21)
GUICtrlSetData($inpPthExe, $srvPthExe)
$lblFilExe = GUICtrlCreateLabel('/' & $script & '.exe', 268, 112, 89, 17)
$lblPthUpd = GUICtrlCreateLabel('Path to update file:', 16, 136, 93, 17)
$srvPthUpd = InpPth($srvPthUpd)
$inpPthUpd = GUICtrlCreateInput('', 112, 132, 153, 21)
GUICtrlSetData($inpPthUpd, $srvPthUpd)
$lblFilUpd = GUICtrlCreateLabel('/' & $script & '.upd', 268, 136, 90, 17)
GUICtrlCreateGroup('', -99, -99, 1, 1)
$btnIni = GUICtrlCreateButton('View ' & $script & '.&ini', 4, 164, 115, 25)
$btnFilUpd = GUICtrlCreateButton('View ' & $script & '.&upd', 126, 164, 119, 25)
If FileExists($filUpd) Then
    GUICtrlSetState($btnFilUpd, $GUI_ENABLE)
Else
    GUICtrlSetState($btnFilUpd, $GUI_DISABLE)
EndIf
$btnSrc = GUICtrlCreateButton('&Show source code', 252, 164, 111, 25)
Dim $frmMain_AccelTable[1][2] = [['^q', $mnuFilQuit]]
GUISetAccelerators($frmMain_AccelTable)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

UpdateCheck()
AdlibRegister('UpdateCheck', 60000)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $mnuFilQuit
            Exit
        Case $mnuFilUnins
            Uninstall()
        Case $mnuHlpAbt
            About()
        Case $radChkFrq0
            GUICtrlSetState($radChkFrq0, $GUI_CHECKED)
            $chkFrq = 86400
        Case $radChkFrq1
            GUICtrlSetState($radChkFrq1, $GUI_CHECKED)
            $chkFrq = 60
        Case $cbxMsgShw
            MsgShw()
        Case $inpPthExe
            $srvPthExe = GUICtrlRead($inpPthExe)
            $srvPthExe = InpPth($srvPthExe)
            GUICtrlSetData($inpPthExe, $srvPthExe)
            $wPos = WinGetPos($title)
            ToolTip('Saved path to .exe file.', $wPos[0] + 115, $wPos[1] + 180)
            Sleep(1500)
            ToolTip('')
        Case $inpPthUpd
            $srvPthUpd = GUICtrlRead($inpPthUpd)
            $srvPthUpd = InpPth($srvPthUpd)
            GUICtrlSetData($inpPthUpd, $srvPthUpd)
            $wPos = WinGetPos($title)
            ToolTip('Saved path to .upd file.', $wPos[0] + 115, $wPos[1] + 204)
            Sleep(1500)
            ToolTip('')
        Case $btnIni
            Run('notepad.exe ' & $ini)
        Case $btnFilUpd
            Run('notepad.exe ' & $filUpd)
        Case $btnSrc
            SrcShow()
    EndSwitch
WEnd

Func MsgShw()
    If $msgShw = 1 Then
        GUICtrlSetState($cbxMsgShw, $GUI_UNCHECKED)
        $msgShw = 0
    Else
        GUICtrlSetState($cbxMsgShw, $GUI_CHECKED)
        $msgShw = 1
    EndIf
EndFunc ;==>MsgShw

Func Install()
    If @Compiled Then
        If Not FileExists($dirMonter) Then DirCreate($dirMonter)
        FileChangeDir($dirMonter)
        If @ScriptDir <> $dirMonter Then
            $del = 1
            $instCpyMovTxt = 'Moving'
            If StringLeft(@ScriptDir, 3) <> StringLeft(@AppDataDir, 3) Then
                $del = 0
                $instCpyMovTxt = 'Copying'
            EndIf
            msg('Launching script for the first time.' & @CRLF & $instCpyMovTxt & ' file into the proper localization, wait a moment...')
            $list = ProcessList($script & '.exe')
            For $i = 1 To $list[0][0]
                $pid = WinGetProcess($script)
                ProcessClose($pid)
            Next
            Sleep(1000)
            FileCopy(@ScriptFullPath, $dirMonter & '\' & $script & '.exe', 9)
            If Not FileExists(@ProgramsDir & '\monter.FM\') Then DirCreate(@ProgramsDir & '\monter.FM\')
            FileCreateShortcut($dirMonter & '\' & $script & '.exe', @ProgramsDir & '\monter.FM\' & $fnScript & '.lnk', $dirMonter, '', $title, $dirMonter & '\' & @ScriptName, '', 4)
            msg()
            $mb = MsgBox(36, $title, 'Do you want the shortcut on the desktop?', 12)
            If $mb = 6 Or $mb = -1 Then FileCreateShortcut($dirMonter & '\' & $script & '.exe', @DesktopDir & '\' & $fnScript & '.lnk', $dirMonter, '', $title, $dirMonter & '\' & @ScriptName, '', 4)
            If $mb = 7 And FileExists(@DesktopDir & '\' & $fnScript & '.lnk') Then FileDelete(@DesktopDir & '\' & $fnScript & '.lnk')
            $baTemp = FileOpen($filBat, 1)
            If $del = 1 Then FileWriteLine($baTemp, 'ping -n 4 autoitscript.com' & @CRLF & 'if exist "' & @ScriptFullPath & '" del "' & @ScriptFullPath & '"')
            FileWriteLine($baTemp, 'ping -n 2 autoitscript.com' & @CRLF & 'if exist "' & $dirMonter & '\' & $script & '.exe' & '" "' & $dirMonter & '\' & $script & '.exe' & '" -h' & @CRLF & 'exit' & @CRLF & 'cls')
            FileClose($baTemp)
            If FileExists($ini) Then FileDelete($ini)
            Sleep(2000)
            Run(@ComSpec & ' /c "' & $filBat & '" -u', '', @SW_HIDE)
            For $i = 1 To 2
                If ProcessExists(@ScriptName) Then ProcessClose(@ScriptName)
                Sleep(2000)
            Next
            $iniDel = 1
            Exit
        EndIf
    EndIf
EndFunc ;==>Install

Func UpdateIcons()
    MsgBox(64, $title, 'You have just launched the new version of ' & $fnScript & ' ' & $sVer, 10)
    FileCreateShortcut($dirMonter & '\' & $script & '.exe', @ProgramsDir & '\monter.FM\' & $fnScript & '.lnk', $dirMonter, '', $title, $dirMonter & '\' & $script & '.exe', '', 4)
    If FileExists(@DesktopDir & '\' & $fnScript & '.lnk') Then
        FileCreateShortcut($dirMonter & '\' & $script & '.exe', @DesktopDir & '\' & $fnScript & '.lnk', $dirMonter, '', $title, $dirMonter & '\' & $script & '.exe', '', 4)
    Else
        $mb = MsgBox(36, $title, 'Do you want the shortcut on the desktop?', 12)
        If $mb = 6 Or $mb = -1 Then FileCreateShortcut($dirMonter & '\' & $script & '.exe', @DesktopDir & '\' & $fnScript & '.lnk', $dirMonter, '', $title, $dirMonter & '\' & $script & '.exe', '', 4)
        If $mb = 7 And FileExists(@DesktopDir & '\' & $fnScript & '.lnk') Then FileDelete(@DesktopDir & '\' & $fnScript & '.lnk')
    EndIf
EndFunc ;==>UpdateIcons

Func UpdateIni() ; here you can convert/modify/delete older ini entries to the current format
    IniWrite($ini, 'Main', 'version', $sVer)
    IniWrite($ini, 'Updater', 'rcntUpdChk', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
EndFunc ;==>UpdateIni

Func IniCheck()
    If IniRead($ini, 'Main', 'version', '') = '' Then IniWrite($ini, 'Main', 'version', $sVer)
    If IniRead($ini, 'Main', 'savedPos', '') = '' Then IniWrite($ini, 'Main', 'savedPos', 'default|default')
    If IniRead($ini, 'Main', 'chkFrq', '') = '' Then IniWrite($ini, 'Main', 'chkFrq', 86400)
    If IniRead($ini, 'Main', 'msgShw', '') = '' Then IniWrite($ini, 'Main', 'msgShw', 0)
    If IniRead($ini, 'Updater', 'srvPthExe', '') = '' Then IniWrite($ini, 'Updater', 'srvPthExe', 'monter.homeip.net/skrypty/bin/' & $script & '.exe')
    If IniRead($ini, 'Updater', 'srvPthUpd', '') = '' Then IniWrite($ini, 'Updater', 'srvPthUpd', 'monter.homeip.net/skrypty/bin/' & $script & '.upd')
    If IniRead($ini, 'Updater', 'rcntUpdChk', '') = '' Then IniWrite($ini, 'Updater', 'rcntUpdChk', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
    If IniRead($ini, 'Updater', 'rcntUsed', '') = '' Then IniWrite($ini, 'Updater', 'rcntUsed', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
EndFunc ;==>IniCheck

Func IniFRead()
    $pos = StringSplit(IniRead($ini, 'Main', 'savedPos', 'default|default'), '|')
    $chkFrq = IniRead($ini, 'Main', 'chkFrq', 86400)
    $msgShw = IniRead($ini, 'Main', 'msgShw', 0)
    $srvPthExe = IniRead($ini, 'Updater', 'srvPthExe', 'monter.homeip.net/skrypty/bin/' & $script & '.exe')
    $srvPthUpd = IniRead($ini, 'Updater', 'srvPthUpd', 'monter.homeip.net/skrypty/bin/' & $script & '.upd')
    $rcntUpdChk = IniRead($ini, 'Updater', 'rcntUpdChk', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
    $rcntUsed = IniRead($ini, 'Updater', 'rcntUsed', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
EndFunc ;==>IniFRead

Func InpPth($pth)
    $ext = StringRight(Eval('pth'), 3)
    $pth = StringReplace($pth, '\', '/')
    If StringInStr($pth, '://', 0, 1) <> 0 Then $pth = StringTrimLeft($pth, StringInStr($pth, '://', 0, 1) + 2)
    If StringInStr($pth, $script & '.' & $ext) <> 0 Then $pth = StringTrimRight($pth, StringLen($script & '.' & $ext))
    If StringRight($pth, 1) = '/' Then $pth = StringTrimRight($pth, 1)
    Return $pth
EndFunc ;==>InpPth

Func UpdateCheck()
    If $msgShw = 1 Then msg('Checking for previous update checking time: ' & _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()) - $rcntUpdChk & ' seconds ago.', -2500)
    If _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()) - $rcntUpdChk >= $chkFrq Or $rcntUsed = $rcntUpdChk Then
        $rcntUpdChk = _DateDiff('s', '1980/01/01 00:00:00', _NowCalc())
        $srvPth = StringTrimRight($srvPthUpd, StringInStr($srvPthUpd, '/', 0, -1))
        $pcol = 'ftp://'
        For $i = 1 To 2 ;you may add proxy server (1 To 3)
            If $msgShw = 1 Then msg('Checking update on server, wait a moment... (' & $i & '/2)' & @CRLF & 'Downloading file ' & $pcol & $srvPthUpd & '/' & $script & '.upd')
            InetGet($pcol & $srvPthUpd & '/' & $script & '.upd', $filUpd, 1, 0)
            Sleep(1500)
            If FileExists($filUpd) And FileGetSize($filUpd) > 30 Then ExitLoop
            If $i = 1 Then $pcol = 'http://'
            ;If $i = 2 Then HttpSetProxy(2, $serverUrl & ':8080')
        Next
        If FileExists($filUpd) Then
            GUICtrlSetState($btnFilUpd, $GUI_ENABLE)
        Else
            GUICtrlSetState($btnFilUpd, $GUI_DISABLE)
        EndIf
        ToolTip('')
        ;HttpSetProxy(1)
        If Not FileExists($filUpd) Then Sleep(2500)
        If Not FileExists($filUpd) Or FileGetSize($filUpd) < 30 Then
            msg('Network error. Update failed.', -4000, -1, -1, -1, 3)
            $rcntUpdChk = _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()) - $rcntUpdChk + Int($chkFrq / 20)
        Else
            $nVer = IniRead($filUpd, $script & '.exe', 'version', '0.0.0.0')
            $nVer = StringFormat('%.2f', Number(StringReplace($nVer, '.', '')) / 1000)
            $nSize = IniRead($filUpd, $script & '.exe', 'size', '999999999')
            If $nVer > $sVer Then
                $pcol = 'ftp://'
                $nFile = IniReadSectionNames($filUpd)
                $nFile = $nFile[1]
                For $i = 1 To 2 ; (+ proxy: 1 To 3)
                    If $msgShw = 1 Then
                        msg('Downloading the newest version, wait a moment... (' & $i & '/2)' & @CRLF & $pcol & $srvPthExe & '/' & $nFile & '(' & $nSize & ' b)')
                    Else
                        msg('Downloading the newest version, wait a moment... (' & $i & '/2)')
                    EndIf
                    InetGet($pcol & $srvPthExe & '/' & $nFile, @TempDir & '\' & $script & '.exe', 1, 0)
                    If FileGetSize(@TempDir & '\' & $script & '.exe') = $nSize Then ExitLoop
                    If $i = 1 Then $pcol = 'http://'
                    ;If $i = 2 Then HttpSetProxy(2, $serverUrl & ':8080')
                Next
                If FileGetSize(@TempDir & '\' & $script & '.exe') < IniRead($filUpd, $script & '.exe', 'size', 0) Then
                    msg('Downloading the newest version failed. Check Internet settings.', -3000, -1, -1, -1, 3)
                    $rcntUpdChk = _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()) - $rcntUpdChk + Int($chkFrq / 20)
                Else
                    If FileExists($filBat) Then FileDelete($filBat)
                    Sleep(1000)
                    $baTemp = FileOpen($filBat, 1)
                    FileWriteLine($baTemp, '@echo off' & @CRLF & 'echo  Updating in progress ' & $script & '...' & @CRLF & 'ping -n 6 autoitscript.com' & @CRLF & 'if exist "' & @TempDir & '\' & $script & '.exe" del "' & @ScriptDir & '\' & $script & '.exe"' & @CRLF & 'move "' & @TempDir & '\' & $script & '.exe" "' & @ScriptDir & '\"')
                    FileWriteLine($baTemp, 'ping -n 3 autoitscript.com' & @CRLF & 'if exist "' & @ScriptDir & '\' & $script & '.exe" "' & @ScriptDir & '\' & $script & '.exe" -u' & @CRLF & 'exit' & @CRLF & 'cls')
                    FileClose($baTemp)
                EndIf
                If FileExists(@TempDir & '\' & $script & '.exe') And FileGetSize(@TempDir & '\' & $script & '.exe') = $nSize Then
                    msg('Update finished succesfully. Launching the new version ' & $fnScript & ' ' & $nVer & '...', -3000, -1, -1, -1)
                Else
                    msg("The new script's filesize is not proper. Something went wrong.", -3000, -1, -1, -1, 2)
                EndIf
                $rcntUpdChk = _DateDiff('s', '1980/01/01 00:00:00', _NowCalc())
                IniWrite($ini, 'Updater', 'rcntUpdChk', $rcntUpdChk)
                If FileExists($filUpd) Then FileDelete($filUpd)
                $updFrq = 86400000
                Run(@ComSpec & ' /c "' & $filBat & '" -u', '', @SW_HIDE)
                If ProcessExists($script & '.exe') Then ProcessClose($script & '.exe')
                Exit
            EndIf
        EndIf
        IniWrite($ini, 'Updater', 'rcntUpdChk', $rcntUpdChk)
    EndIf
    $rcntUsed = _DateDiff('s', '1980/01/01 00:00:00', _NowCalc())
EndFunc ;==>UpdateCheck

Func SrcShow()
    $srvPthSrc = StringLeft($srvPthExe, StringInStr($srvPthExe, '/', 0, -1) - 1)
    If Not FileExists($src) Then
        If $msgShw = 1 Then
            msg('Downloading the source code, wait a moment...' & @CRLF & 'http://' & $srvPthSrc & '/src/' & $script & '.au3')
        Else
            msg('Downloading the source code, wait a moment...')
        EndIf
        InetGet('http://' & $srvPthSrc & '/src/' & $script & '.au3', $src, 1)
        Sleep(500)
    EndIf
    Run('notepad.exe ' & $src)
    msg()
EndFunc ;==>SrcShow

Func Uninstall()
    If @Compiled Then
        $uninQry = MsgBox(308, $title, 'Are you sure you want to uninstall/delete ' & $fnScript & ' from disk?', 12)
        If $uninQry = 6 Then
            msg('Uninstalling ' & $title & ' in progress...')
            GUIDelete($frmMain)
            If FileExists($ini) Then FileDelete($ini)
            If FileExists(@ProgramsDir & '\monter.FM\' & $fnScript & '.lnk') Then FileDelete(@ProgramsDir & '\monter.FM\' & $fnScript & '.lnk')
            If FileExists(@DesktopDir & '\' & $fnScript & '.lnk') Then FileDelete(@DesktopDir & '\' & $fnScript & '.lnk')
            If Not FileExists(@ProgramsDir & '\monter.FM\*.*') Then DirRemove(@ProgramsDir & '\monter.FM')
            If FileExists($filBat) Then FileDelete($filBat)
            Sleep(1000)
            $baTemp = FileOpen($filBat, 1)
            FileWriteLine($baTemp, 'echo    Uninstall in progress: ' & $title & '...' & @CRLF & ':loop' & @CRLF & 'ping -n 3 autoitscript.com' & @CRLF & 'del "' & @ScriptDir & '\' & $script & '.exe"')
            FileWriteLine($baTemp, 'ping -n 2 autoitscript.com' & @CRLF & 'if exist "' & @ScriptDir & '\' & $script & '.exe" goto loop')
            FileWriteLine($baTemp, 'ping -n 4 autoitscript.com' & @CRLF & 'del "' & $filBat & '"' & @CRLF & 'exit' & @CRLF & 'cls')
            FileClose($baTemp)
            Sleep(1000)
            Run(@ComSpec & ' /c ' & $filBat, '', @SW_HIDE)
            If ProcessExists(@ScriptName) Then ProcessClose(@ScriptName)
            $iniDel = 1
            Exit
        Else
            msg('Uninstall aborted.', -2500)
        EndIf
    Else
        msg('Compiled ' & $title & ' would be uninstalled now.' & @CRLF & 'Quitting the script.', 2500)
        Exit
    EndIf
EndFunc ;==>Uninstall

Func FileInfo()
    If @Compiled Then ;section identifying script's version and release date from #AutoIt3Wrapper fields :-)
        Global $sVer = StringFormat('%.2f', Number(StringReplace(FileGetVersion(@ScriptFullPath), '.', '')) / 1000) ;script's version in x.xx format
        Global $dateRlse = FileGetVersion(@ScriptFullPath, 'Release date')
    Else
        Opt('TrayIconHide', 0)
        Opt('TrayIconDebug', 1)
        $strRes = '#AutoIt3Wrapper_'
        For $ln = 1 To 50
            $srchRes = StringInStr(FileReadLine(@ScriptFullPath, $ln), $strRes)
            If $srchRes > 0 Then
                $lr = $ln
                ExitLoop
            EndIf
        Next
        $strRes = '#AutoIt3Wrapper_Res_Fileversion='
        Dim $lnRes[3]
        For $i = 1 To 2
            For $ln = $lr To $lr + 16
                $srchRes = StringInStr(FileReadLine(@ScriptFullPath, $ln), $strRes)
                If $srchRes > 0 Then
                    $lnRes[$i] = $ln
                    ExitLoop
                EndIf
            Next
            $strRes = '#AutoIt3Wrapper_Res_Field=Release date|'
        Next
        Global $sVer = StringFormat('%.2f', Number(StringReplace(StringTrimLeft(FileReadLine(@ScriptFullPath, $lnRes[1]), 32), '.', '')) / 1000)
        $rd = FileGetTime(@ScriptFullPath, 0, 0)
        _FileWriteToLine(@ScriptFullPath, $lnRes[2], '#AutoIt3Wrapper_Res_Field=Release date|' & $rd[2] & '.' & $rd[1] & '.' & $rd[0], 1)
        Global $dateRlse = $rd[2] & '.' & $rd[1] & '.' & $rd[0]
        FileSetTime(@ScriptFullPath, $rd[0] & $rd[1] & $rd[2] & $rd[3] & $rd[4] & $rd[5], 0)
    EndIf
EndFunc ;==>FileInfo

Func About()
    $frmAbout = GUICreate($title & ' - About', $wi + 20, 120, -1, -1, -1, BitOR($WS_EX_WINDOWEDGE, $WS_EX_APPWINDOW), $frmMain)
    GUICtrlCreateLabel(StringFormat('Example of auto-update function in AutoIt3 script.'), 25, 8, 292, 69)
    GUICtrlCreateLabel($title & ' built on AutoIt ' & @AutoItVersion, 25, 28, 284, 35)
    GUICtrlCreateLabel($dateRlse, 300, 48, 50, 15)
    GUICtrlSetFont(-1, 7, 400, 0, 'Tahoma')
    $btnOkAbt = GUICtrlCreateButton('OK', 174, 90, 51, 25)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        If $nMsg = $GUI_EVENT_CLOSE Or $nMsg = $btnOkAbt Then ExitLoop
    WEnd
    GUIDelete($frmAbout)
EndFunc ;==>About

Func msg($txt = '', $ms = 1500, $title = -1, $ttX = -1, $ttY = -2, $icn = 1, $tray = -1)
    If $ms >= 0 And $ms < 250 Then $ms = 250
    If $ms = -1 Then $ms = 1500
    If $ms < -1 Then
        $ms = Abs($ms)
        $clr = 1
    EndIf
    If $title = -1 Then
        $scN = StringSplit(@ScriptName, '.')
        $title = $scN[1]
        If IsDeclared('fnScript') And IsDeclared('sVer') Then $title = $fnScript & ' ' & $sVer
    EndIf
    If $ttX = -1 Then $ttX = Int(@DesktopWidth / 2)
    If $ttY = -1 Then $ttY = Int(@DesktopHeight / 2)
    If $ttY = -2 Then $ttY = @DesktopHeight - 64
    If $icn = 2 Then $txt = 'Warning!' & @CRLF & $txt
    If $icn = 3 Then $txt = 'ERROR!' & @CRLF & $txt
    If $tray = -1 Then ToolTip($txt, $ttX, $ttY, $title, $icn, 2)
    If $tray = 1 Then TrayTip($title, $txt, $ms, $icn)
    Sleep($ms)
    If IsDeclared('clr') Then ToolTip('')
EndFunc ;==>msg

Func OnExit()
    BlockInput(0)
    If FileExists($filUpd) Then FileDelete($filUpd)
    If IsDeclared('src') Then FileDelete($src)
    If $iniDel = 0 Then
        IniWrite($ini, 'Main', 'chkFrq', $chkFrq)
        IniWrite($ini, 'Main', 'msgShw', $msgShw)
        IniWrite($ini, 'Updater', 'rcntUsed', _DateDiff('s', '1980/01/01 00:00:00', _NowCalc()))
        IniWrite($ini, 'Updater', 'srvPthExe', $srvPthExe & '/' & $script & '.exe')
        IniWrite($ini, 'Updater', 'srvPthUpd', $srvPthUpd & '/' & $script & '.upd')
        If IsDeclared('pos') Then
            $wPos = WinGetPos($title)
            If $wPos[0] <> -32000 Then IniWrite($ini, 'Main', 'savedPos', $wPos[0] & '|' & $wPos[1])
        EndIf
    EndIf
EndFunc ;==>OnExit

AutoUpdater.au3

Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

I think you should add an input for user type their custom prequency. Such as : 10 minutes, 20 minutes,...

Thanks about an useful script! :idea:

Link to comment
Share on other sites

I think you should add an input for user type their custom prequency. Such as : 10 minutes, 20 minutes,...

I think it's not so necessary option. I will set every 24h checking in my scripts, 60s is only to show how it works (with all messages option checked)

Thanks about an useful script! :idea:

Thanks! :) Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

Script updated. Main fix makes updating possible now ;-)

See first post.

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

The script is updated (version 0.30) - improved checking intervals, more messages in "show ALL messages" mode.

As usual => See first post.

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

  • 2 weeks later...

0.40 => improved working in AdLib module - variable checking frequency. After launching the script - 4s, then 24h/60s (depends on settings).

Update is available only with this procedure - download 0.20 compiled version, after this it will auto-update and auto-relaunch the newest version with the newest available source (see first post).

Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

  • 3 weeks later...

0.50 => added icon, several fixes... ==> first post.

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

  • 1 month later...

Howdy,

I'm trying to impliment this into a program I've been creating. It appears like the files on your server for the example are no longer there. What should the .upd contain? Is that essentially an .ini with a different extension?

Thanks,

Link to comment
Share on other sites

Howdy,

I'm trying to impliment this into a program I've been creating. It appears like the files on your server for the example are no longer there. What should the .upd contain? Is that essentially an .ini with a different extension?

Thanks,

Recently my home PC (which is FTP/WWW server also) is in big trouble. My hard disk (track 0) failed and system can't stand up.

Yes, it works with IniRead/IniWrite, but the file has .upd extension. The .upd file contains:

[AutoUpdater.exe]
version=0.5.0.0
size=367084
datetime=20100605134735

:blink:

Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

Temporarily my home server is kaput.

To get back working script just download the older version => AutoUpdater 030, input the different paths to script and upd files:

monter.fm/bin/scripts
.

After auto-update you have to input new paths again (it's a bug :blink: ).

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

[OT] I bought new hard disk and before I replaced it - the old one (defective) started working ;) Now (maybe temporarily) the original server is up :blink:

BTW I have few new TODOs:

1. Add an secondary (additional) server/path if the primary is down.

2. Checking for update should be run in a different process - currently if the server is down, script's work is frozen (I thought AdLibRegister is a kind of multitasking stuff).

Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Link to comment
Share on other sites

Awesome, thank you. I've got it working in tests. If you ever need an ftp that's quick, painless, yet very powerful and has builtin html interface/access to it, I highly recommend HFS , I've been using it for a long time on both personal and company servers. It's awesome for file transfering with clients who aren't familiar with ftp.

Here's a couple of friendly suggestions/ideas I have for your script (great work btw):

Make it into a UDF, with minimal GUI (also have an argument to run it gui-less). For my script (which is getting rather large, so not needing to rip out pieces would be awesome) the UDF would ideally have a function that can be called on startup or by a menu item that checks for a new version and if one exists, prompts the user if they'd like to update it to the new one (also include an argument that skips the prompt and always updates if a new one exists which would be nice for apps that are in alpha/beta). For larger apps, having servers hard-coded instead of in an .ini would also be nice. So the goal would be to have the user's script just need:

#include <AutoUpdater.au3>

; in case their users rename it
Global $AU_ScriptName = 'ScriptName'

; if they want server path to be hard-coded
Global $AU_SrvPrimary = 'primaryurl.com/appname/'
Global $AU_SrvSecondary = 'secondaryurl.com/appname/' ;optional
;or if they'd prefer to use an .ini
Global $AU_Ini = 'path\and\name\of.ini'

; to allow the user to choose @ProgramFilesDir instead of @AppDataDir if they desire
; as well as choose the folder name (need to replace all 
Global $AU_InstallDir = @AppDataDir & '\foldername'

_AU_CheckUpdate($iInterval = 0, $bPrompt = True)

$iInterval would be the time between checks for your Process/AdLibRegister, or if 0, would check immediately when the function is called and not spawn a process/AdLibRegister. $bPrompt would switch between asking the user's user if they they want to update if true, or do a silent update if false.

If the user prefers to not use an .ini (for example if their script uses .ini's heavily and adding new sections may cause conflicts in functions they wrote), you can store things like rcntUpdChk and rcntUsed to the registry. Here's 2 functions (slightly modified to work with the variables i wrote above) borrowed from seangriffin's "ChildProc.au3" & Florian 'Piccaso' Fida's "CoProc.au3" (which are great UDF's for dealing with multi-processes):

; #FUNCTION# ;===============================================================================
;
; Name...........: _WriteToRegistry()
; Description ...:  Stores data from a process into the Windows Registry, for the purposes of
;                   inter-process communication between a child process and the parent script.
; Syntax.........: _WriteToRegistry($sName, $vValue = Default, $sRegistryBase = Default)
; Parameters ....:  $sName              - Identifier for the registry key
;                   $vValue             - (optional) Value to be stored
;                   $sRegistryBase      - (optional) Registry Base Key
; Return values .:  On Success          - Returns True.
;       On Failure          - Returns False.
; Author ........:  seangriffin
; Modified.......:
; Remarks .......:  Florian 'Piccaso' Fida ("_SuperGlobalSet" from the CoProc UDF with minor changes by seangriffin)
; Related .......:
; Link ..........:
; Example .......:  Yes
;
; ;==========================================================================================
Func _WriteToRegistry($sName, $vValue = Default, $sRegistryBase = Default)

    Local $vTmp

    $gs_SuperGlobalRegistryBase = "HKEY_CURRENT_USER\Software\AutoIt v3\" & $AU_ScriptName
    If $sRegistryBase = Default Then $sRegistryBase = $gs_SuperGlobalRegistryBase

    If $vValue = "" Or $vValue = Default Then

        RegDelete($sRegistryBase, $sName)
        return True
    EndIf

    If @error Then

        Return SetError(2, 0, False) ; Registry Problem
    Else

        RegWrite($sRegistryBase, $sName, "REG_BINARY", StringToBinary ($vValue))

        If @error Then Return SetError(2, 0, False) ; Registry Problem
    EndIf

    Return True
EndFunc

; #FUNCTION# ;===============================================================================
;
; Name...........:  _ReadFromRegistry()
; Description ...:  Gets data from a key in the Windows Registry, for the purposes of
;                   inter-process communication between a child process and the parent script.
; Syntax.........:  _ReadFromRegistry($sName, $fOption = 1, $sRegistryBase = Default)
; Parameters ....:  $sName              - Identifier for the registry key
;                   $fOption            - (optional) If True, the registry key will be deleted after sucessfully reading it
;                   $sRegistryBase      - (optional) Registry Base Key
; Return values .:  On Success          - Returns the text from the registry key.
;       On Failure          - Returns False.
; Author ........:  seangriffin
; Modified.......:
; Remarks .......:  Florian 'Piccaso' Fida ("_SuperGlobalGet" from the CoProc UDF with minor changes by seangriffin)
; Related .......:
; Link ..........:
; Example .......:  Yes
;
; ;==========================================================================================
Func _ReadFromRegistry($sName, $fOption = Default, $sRegistryBase = Default)

    Local $vTmp

    $gs_SuperGlobalRegistryBase = "HKEY_CURRENT_USER\Software\AutoIt v3\" & $AU_ScriptName
    If $fOption = "" Or $fOption = Default Then $fOption = False

    If $sRegistryBase = Default Then $sRegistryBase = $gs_SuperGlobalRegistryBase

    $vTmp = RegRead($sRegistryBase, $sName)

    If @error Then Return SetError(1, 0, "") ; Registry Problem

    If $fOption Then

        _WriteToRegistry($sName)

        If @error Then SetError(2)
    EndIf

    Return BinaryToString($vTmp)
EndFunc

Other things like the user's user's version and size of the .exe can be checked on the fly with

FileGetVersion(@ScriptFullPath)
FileGetSize(@ScriptFullPath)

btw, when uninstall is run currently, it doesn't delete the folder created in the start menu (though it does delete the link inside it). A good idea might be when installing and creating the start menu folder and link, it also adds an uninstall link that calls the script with a cmdline argument to run the uninstall function.

Just some food for thought.

Cheers,

:blink:

Link to comment
Share on other sites

Another idea: I don't really know much about net stuff, but i bet there's some way using an .xml or something instead of a .upd so that it can read the newest version number straight from the web without needing to download a file. Which would speed up checking. Another cool thing would be to make another .exe that can be put in the 'Run After' section of the scite compiler, and would prompt the user once compiling is done if they want to upload it to the server. If they choose yes, it'd then upload the new compiled file and auto-generate a matching .upd/.xml and upload that too.

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