Jump to content
Sign in to follow this  
Blue_Drache

Yet Another Silly Update Script

Recommended Posts

Blue_Drache

I hope that this finds some use! It's commented rather well. Enjoy.

Edit: Quick little observation: This is more of a proof of concept for myself, but it does work over a LAN rather well. I'm sure it wouldn't take much to convert to enable auto-update over a WAN by connecting to the web resource, downloading the file and then examining it. Oh..that's NOT in the plan, I'd have to make a toggle switch and modify the code.

#include <Date.au3>

_AutoUpdate($CmdLine, "C:\FOLDER\FILE.EXE", "X:\FOLDER\FILE.EXE")

Func _AutoUpdate($sz_CommandParam, $sz_fileSelf, $sz_fileRemote)
    Local $sz_dateSelf = "", $sz_dateRemote = ""
    Local $funcTemp = "", $t = "", $sz_WinTitleOld = AutoItWinGetTitle()
    AutoItWinSetTitle("AutoUpdate")
    If Not FileExists($sz_fileRemote) Then
        MsgBox(0, "AutoUpdate", "Error#:187" & @LF & "Update file not found.  Program will continue normally")
        SetError(0)
        SetExtended(1)
        AutoItWinSetTitle($sz_WinTitleOld)
        Return 0
    EndIf
    ; check here for command line parameters
    If $sz_CommandParam[0] > 0 Then ; commandline passed!
        If $sz_CommandParam[1] = "-update" Then
            ; 5a) If -update has been called, this is the temp file running.
            ;    Copy the remote to the user's normal local folder, run the updated local, self-delete and exit.
            FileCopy($sz_fileRemote, $sz_fileSelf, 1)
            Run($sz_fileSelf)
            _SelfDelete()
            Exit
        Else
            ; 5b) If any other command line parameter has been called.   Die.  Horribly.
            MsgBox(0, "AutoUpdate", "Bad Command Line Parameter(s)" & @LF & "I only accept -update at this time.")
            Exit
        EndIf
    Else ; no command line passed
        ; 1) Get file date on self.
        $t = FileGetTime($sz_fileSelf, 1)
        If Not @error Then
            $sz_dateSelf = $t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5]
        Else
            AutoItWinSetTitle($sz_WinTitleOld)
            SetError(1)
            Return 1
        EndIf
        ; 2) Get file date on file in remote location
        $t = FileGetTime($sz_fileRemote, 1)
        If Not @error Then
            $sz_dateRemote = $t[0] & "/" & $t[1] & "/" & $t[2] & " " & $t[3] & ":" & $t[4] & ":" & $t[5]
        Else
            AutoItWinSetTitle($sz_WinTitleOld)
            SetError(2)
            Return 1
        EndIf
        ; 3) Determine if remote is newer
        $t = _DateDiff('s', $sz_dateSelf, $sz_dateRemote)
        If @error Then
            AutoItWinSetTitle($sz_WinTitleOld)
            SetError(3)
            Return 1
        EndIf
        Switch $t
            Case - 3600 To 3600
                ; 4a) If the remote is an hour or less different, don't bother.
                AutoItWinSetTitle($sz_WinTitleOld)
                Return 0
            Case Else
                ; 4b) Otherwise, copy remote into temp folder with random name.
                ;     Execute the temp with command line parameter and terminate.
                $funcTemp = Random(1001, 9999, 1)
                FileCopy($sz_fileRemote, @TempDir & "\AUD-" & $funcTemp & ".exe")
                Run(@ComSpec & " /c " & @TempDir & '"\AUD-' & $funcTemp & '.exe" -update', "", @SW_HIDE)
                Exit
        EndSwitch
    EndIf
EndFunc   ;==>_AutoUpdate
Func _SelfDelete($iDelay = 0)
    ; This is from the help file.
    Local $sCmdFile
    FileDelete(@TempDir & "\scratch.bat")
    $sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
             & ':loop' & @CRLF _
             & 'del "' & @ScriptFullPath & '" > nul' & @CRLF _
             & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
             & 'del ' & @TempDir & '\scratch.bat'
    FileWrite(@TempDir & "\scratch.bat", $sCmdFile)
    Run(@TempDir & "\scratch.bat", @TempDir, @SW_HIDE)
EndFunc   ;==>_SelfDelete
Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
BillLuvsU

Cool, only suggestion is in the SelfDelete function just rename any possibly already existing "scratch.bat"s to an unused name then rename it back. You know just incase some super inportants prog just happens to use a scratch.bat prog that just happens to be in the temp directory o_0. Then again thats just my over-cautios self talking. Good job again!


[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Share this post


Link to post
Share on other sites
Blue_Drache

Cool, only suggestion is in the SelfDelete function just rename any possibly already existing "scratch.bat"s to an unused name then rename it back. You know just incase some super inportants prog just happens to use a scratch.bat prog that just happens to be in the temp directory o_0. Then again thats just my over-cautios self talking. Good job again!

The self delete function is ripped directly from the help file ... and honestly, nowadays, what program uses batch files? In the temp folder?

I'm sure if you wanted you could put a random filename.

Like this:

Func _SelfDelete($iDelay = 0)
    ; This is from the help file.
    Local $sCmdFile, $sFileName
    $sFileName = "\" & Random(10000001, 99999999, 1) & ".bat"
    FileDelete(@TempDir & $sFileName)
    $sCmdFile = 'ping -n ' & $iDelay & '127.0.0.1 > nul' & @CRLF _
             & ':loop' & @CRLF _
             & 'del "' & @ScriptFullPath & '" > nul' & @CRLF _
             & 'if exist "' & @ScriptFullPath & '" goto loop' & @CRLF _
             & 'del ' & @TempDir & $sFileName
    FileWrite(@TempDir & $sFileName, $sCmdFile)
    Run(@TempDir & $sFileName, @TempDir, @SW_HIDE)
EndFunc   ;==>_SelfDelete

But ... meh. Overkill.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

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  

×