Jump to content

self_update


gcue
 Share

Recommended Posts

hello world.

I am trying to do a self updating script.  I see several examples which use a batch file and taskkill to end the current script and copy the new one.  however, with windows 7 the batch file has to run with elevated rights to taskkill the process successfully and the user running the script may not necessarily have those rights.

has anyone found another way to get around this?

thanks =>

Link to comment
Share on other sites

Is it necessary to update the running script?

You should separate data and code. Data should be changeable by the user, code changes should only be made and tested by a coder/scripter/programmer.

Can you describe your problem in more detail?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

there really isnt data.. so the script changes reflect bug fixes or enhancements to the functionality.

i think i may have thought of a way.

Func Check_Updates()
 
$remote_version = FileGetVersion($tools_dir & "\" & @ScriptName)
$local_version = FileGetVersion(@ScriptFullPath)
 
$compare_versions = _VersionCompare($local_version, $remote_version)
 
If Not @error Then
If $compare_versions = -1 Then
Get_Update()
EndIf
EndIf
 
EndFunc   ;==>Check_Updates
 
Func Get_Update()
 
$remote_file = $tools_dir & "\" & @ScriptName
 
$script_name = StringReplace(@ScriptName, ".exe", "")
$script_name = StringStripWS($script_name, 8)
$batch_file = @TempDir & "\update_" & $script_name & ".cmd"
 
If FileExists($batch_file) Then
FileDelete($batch_file)
EndIf
 
$command = '@echo off' & @CRLF
$command &= '' & @CRLF
$command &= 'ping localhost -n 5 > nul' & @CRLF
$command &= 'del /f /q "' & @ScriptFullPath & '"' & @CRLF
$command &= 'copy /y "' & $remote_file & '" "'& @ScriptFullPath & '"' & @CRLF
$command &= @ScriptFullPath & @CRLF
$command &= 'del %0'
 
FileWrite($batch_file, $command)
 
Run($batch_file, @ScriptDir, @SW_HIDE)
 
_Exit()
 
EndFunc   ;==>Get_Update
Link to comment
Share on other sites

Better idea would be to have a separate script that downloads the updates and then copies the files to the installation location. Run the update script, and exit the current script right after you do that. Then you don't need to force close the main script as it's already closed.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

That would be a simple and reliable way to solve your problem.

If needed you could delete the update script if no update is needed. So no traces are left on the users PC.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

self delete?  thats through batch file tho right?  need the elevated rights to taskkill it first?

or

delete through parent script.  and only delete it if theres no updates - otherwise let it linger on their pc ;-)

Link to comment
Share on other sites

The idea is:

Check if an update is available

If yes:

  • Fileinstall the Update script
  • start the update script
  • exit the main script
  • Now the update is being done by the update script
  • call >_SelfDelete to delete the update script

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

There is a _SelfUpdate() in my signature.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

There are other SelfDelete solutions available that simple write a BAT file to disk, call this bat file and exit.

Shouldn't need elevated permissions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

There is a _SelfUpdate() in my signature.

Your _SelfUpdate uses TaskKill which needs elevated permissions which isn't possible for gcue according to his OP.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Here's what I do if it helps at all :)

I have an installer, packed in the installer is the main script.

The main script (when installed) can search for updates by downloading a txt file in my dropbox and comparing the current version with the last version listed in my text file. 

If there is a newer version in the text file, the main program prompts a download.

The new download is the main program packed in an installer.

The installer (when run) ProcessCloses any instances of the main program running and extracts the new files.

Then it starts the (new) main program and exits itself.

Update done.

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

Your _SelfUpdate uses TaskKill which needs elevated permissions which isn't possible for gcue according to his OP.

They can delete that line if they so wish.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I do a version updating script here for our users for launching our in house web based accounting system and our legacy term window system.

I have two files, the launcher and an update script. I tossed a file (ini file which includes the versions of both on the network, just for information.  If this file exists I allow the script to version check both).  This allows me to remove this file to stop any updating if it goes bonkers.

On startup I check the local update.exe version against the server - this is just a filecopy.  I also check the launcher file against the server version, if the server one is new I copy it over to a temp name, launch the update.exe with a parameter (just so you can't run it by itself) and close out of the launcher.  When update.exe starts, it looks for the process that you pass to it, when that terms, It replaces the launcher file with the temp file it downloaded.  I cache the version in an ini file local as well so I can version compare to find out if it's first run, that way I can do first run versioning stuff (sometimes they download a new icon or I change an ODBC connection).

Works really well, i've done over a dozen version changes with no oddball behavior.

Link to comment
Share on other sites

 

The idea is:

Check if an update is available

If yes:

  • Fileinstall the Update script
  • start the update script
  • exit the main script
  • Now the update is being done by the update script
  • call >_SelfDelete to delete the update script

Why not

Parent fileinstall update script

parent close

update script updates

update starts parent

update script closes as parent sleeps for an amount of time (~200ms)

parent deletes update script

Or you could have the update script launch the parent script with a command line peram that tells the parent to delete it and then close, otherwise parent runs normally

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Great. That seems to be the simplest solution to the OPs "problem" :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

hmm i am getting "update failed to download" (from updater script) approximately 1 out of 10 attempts.  in addition to the error, the parent script dissapears and the updater script remains.

works fine the other 9 times - problem with logic?

parent script

Func Check_Updates()
 
$remote_version = FileGetVersion($tools_dir & "\" & @ScriptName)
$local_version = FileGetVersion(@ScriptFullPath)
 
$compare_versions = _VersionCompare($local_version, $remote_version)
 
If Not @error Then
If $compare_versions = -1 Then
FileInstall("C:\updater.exe", @TempDir & "\updater.exe", 1)
Get_Update()
EndIf
EndIf
 
EndFunc   ;==>Check_Updates
 
Func Get_Update()
 
If FileExists(@TempDir & "\updater.exe") Then
$remote_file = $tools_dir & "\" & @ScriptName
 
Run(@TempDir & '\updater.exe -remote_file "' & $remote_file & '" -local_file "' & @ScriptFullPath & '" -process_exe "' & @ScriptName & '"')
_Exit() 
EndIf
 
EndFunc   ;==>Get_Update

updater script

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=updater.ico
#AutoIt3Wrapper_Outfile=updater.exe
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
 
#include <WindowsConstants.au3>
 
Global $msg_error = 262160
 
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1 + 2)
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "_Exit")
 
TraySetToolTip(StringReplace(@ScriptName, ".exe", ""))
 
TraySetState(1)
 
If $CmdLine[0] > 0 Then
For $i = 1 To $CmdLine[0]
Select
Case $CmdLine[$i] = "-remote_file"
$remote_file = $CmdLine[$i + 1]
Case $CmdLine[$i] = "-local_file"
$local_file = $CmdLine[$i + 1]
Case $CmdLine[$i] = "-process_exe"
$process_exe = $CmdLine[$i + 1]
 
UpdateScript($local_file, $remote_file, $process_exe)
EndSelect
Next
EndIf
 
Func UpdateScript($local_file, $remote_file, $process_exe)
 
$process_name = StringReplace($process_exe, ".exe", "")
 
$progress = GUICreate("Please Wait", 200, 70, -1, -1, $WS_EX_MDICHILD, $WS_EX_TOPMOST)
GUISetFont(10, 600)
GUICtrlCreateLabel("Updating...", 10, 10)
 
GUISetState()
 
$log = @TempDir & "\log.txt"
 
$close = Kill_Process($process_exe)
 
If $close = 1 Then
$copy = FileCopy($remote_file, $local_file, 1)
 
If $copy = 0 Then
$copy = FileCopy($remote_file, $local_file, 1)
 
If $copy = 0 Then
FileWrite($log, $close & @CRLF & $copy & @CRLF & $remote_file & @CRLF & $local_file)
MsgBox($msg_error, $process_name, "Update failed to download.")
_Exit()
EndIf
EndIf
 
ShellExecute($local_file)
Else
MsgBox($msg_error, $process_name, "Unable to exit " & $process_name & "." & @CRLF & _
"" & @CRLF & _
"Update canceled.")
EndIf
 
Self_Delete()
 
_Exit()
 
EndFunc   ;==>UpdateScript
 
Func Kill_Process($process_exe)
 
For $x = 1 To 10
If ProcessExists($process_exe) Then
ProcessClose($process_exe)
Else
ExitLoop
EndIf
Next
 
$close = 1
 
If ProcessExists($process_exe) Then
$close = ProcessWaitClose($process_exe, 3)
EndIf
 
Return $close
 
EndFunc   ;==>Kill_Process
 
Func Self_Delete()
 
$batch_file = @TempDir & "\delete_updater.cmd"
 
If FileExists($batch_file) Then
FileDelete($batch_file)
EndIf
 
$command = '@echo off' & @CRLF
$command &= '' & @CRLF
$command &= 'ping localhost -n 3 > nul' & @CRLF
$command &= 'del /f /q "' & @ScriptFullPath & '"' & @CRLF
$command &= 'del %0'
 
FileWrite($batch_file, $command)
 
Run($batch_file, @ScriptDir, @SW_HIDE)
 
EndFunc   ;==>Self_Delete
 
Func _Exit()
Exit
EndFunc   ;==>_Exit
Edited by gcue
Link to comment
Share on other sites

the log shows the right paths as expected.  so based off the return values, the parent process is terminated successfully and the copy command fails twice.

makes sense that the updater file remains because it never goes through the self_delete function but dont understand why the parent script disappears.

if the copy fails when replacing a file, does it delete the original one anyway?

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