Sign in to follow this  
Followers 0
darkjohn20

_UpdateFile

3 posts in this topic

#1 ·  Posted (edited)

Hello,

This is my first UDF made. I know this has probably been done before, but I wanted to start somewhere.

_UpdateFile takes a local file and replaces it with one off the internet. This is useful for self-updating your program if a newer version is found. You must use your own method to check versions. It is not included because there are so many different ways to do it. It may not be perfect, I'm not really sure.

Tested in Win7 Ultimate 32-Bit with a .gif file.

Function:

#include <File.au3>
#include <Constants.au3>

; #FUNCTION# ;===============================================================================
;
; Name...........:  _FileUpdate
; Description ...: Updates a local file with a newer one found on the internet
; Parameters ....: $sOld_File_Path - Local path of the old file "C:\Program Files\Company\OldFile.exe"
; $sNew_File_Web_Path - Internet address for the new file "http://www.somesite.com/files/NewFile.exe"
; Return values .: Success - 1
; Failure - Returns 0 and Sets @Error:
; |1 - Invalid $sOld_File_Path
; |2 - Invalid $sNew_File_Web_Path
; |3 - Could not create .BAT file
; |4 - Could not write to .BAT file
;    |5 - Could not run .BAT file
; Author ........: John Fedorchak
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _FileUpdate($sOld_File_Path, $sNew_File_Web_Path)
    Local $iIsValidPath, $hDownloadFile, $hBAT, $iFileWrite
    Local $sDrive, $sDirectory, $sName, $sExtention

    ;Check if local file exists
    $iIsValidPath = FileExists($sOld_File_Path)
    If $iIsValidPath = 0 Then Return SetError(1)

    ;Check if web file exists
    $iIsValidPath = InetGetSize($sNew_File_Web_Path, 1)
    If $iIsValidPath = 0 Then Return SetError(2)

    ;Download File
    _PathSplit($sNew_File_Web_Path, $sDrive, $sDirectory, $sName, $sExtention)
    $hDownloadFile = InetGet($sNew_File_Web_Path, @TempDir & "\Temporary Update File" & $sExtention, 1)

    ;Create .BAT file
    $hBAT = FileOpen(@TempDir & "\Update.bat", 1)
    If $hBAT = -1 Then Return SetError(3)

    ;Write to .BAT file
    $iFileWrite = FileWrite($hBAT, 'DEL "' & $sOld_File_Path & '"' & @CRLF & _ ;DEL OldFile
                     'COPY "' & @TempDir & '\Temporary Update File' & $sExtention & '" "' & $sOld_File_Path & '"') ;COPY NewFile OldFilePath
    If $iFileWrite = 0 Then Return SetError(4)

    ;Close .BAT writing
    FileClose($hBAT)

    ;Run .BAT file
    $hExecute = Run(@TempDir & "\Update.bat", @TempDir, @SW_HIDE, $STDOUT_CHILD)
    If $hExecute = 0 Then Return SetError(5)

    ;Finished
    Return SetError(0)
EndFunc

Example:

#include "_UpdateFile.au3"

$iReturn = _FileUpdate(@DesktopDir & "\Google.gif", "http://www.google.com/intl/en_ALL/images/logo.gif")
If $iReturn = 1 Then $iReturn = "Successful!"
Switch @error
    Case 0
        $sERROR = "No errors!"
    Case 1
        $sERROR = "Invalid first parameter."
    Case 2
        $sERROR = "Invalid second parameter."
    Case 3
        $sERROR = "Could not create Batch file."
    Case 4
        $sERROR = "Could not write to Batch file."
    Case 5
        $sERROR = "Could not run Batch file."
EndSwitch

MsgBox(0, "Return", $iReturn & @CRLF & $sERROR)

To use this example you must first download Google's "logo.gif" and rename it to "Google.gif".

Also, the forum messed up a little bit of formatting. Let me see if I can fix it.

_UpdateFile Example.au3

_UpdateFile.au3

Edited by darkjohn20

Share this post


Link to post
Share on other sites



Uhm ...

Congratulations started very well ..

Thanks for posting

Grateful

---------- EDIT ------------------

It would be interesting if you put a ProcessOn / set

to display the download process .. would be very interesting

-------Edit--------

Tested here .. I do not doit with trainer

of Parameters invalid all the time


-------------------------------------------------------------------------------------------------------------------------------------------- [center][/center][center]Autoit Support Forum in Portuguese | AutoitBrasil.com[/center] [sub]My Script :[/sub]Simples Login for Program

Share this post


Link to post
Share on other sites

You need to make sure you have the file before you use this example.

For this, go to www.google.com

Go to Tools -> Page Info -> Media and download logo.gif as Google.gif.

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  
Followers 0