# Generic automated updating of desktop applications

Is there an application that will silently update a local folder containing all the files in a web directory? Or even better, if there is a .zip file, it will extract the files into the specified folder?

I have a simple desktop web application (HTML/Javascript using jQuery) in a local folder, and whenever it is updated, I would like the computers in our office to automatically install the update by downloading something like 'latest.zip' automatically (through HTTP download, not FTP download) and let the updater copy the files into the application directory.

I've been searching but all I see are file synchronization which is bi-directional and programs that are too complicated to use or are too heavy. My goal is to be able to silently update the application directory with very minimal user interaction. But since JavaScript can't write on hard drives, it needs to be done using another application (exe file) which will run periodically.

Maybe there is something like a generic updater application in which you can put a web address of the files and the local directory where the files should be copied?

-
Not sure which programs you are looking for, but have you checked out ninite.com? – Kruug Feb 18 '13 at 17:35

Since the question mentions an .exe file, I assume you're on Windows. If so, you can do this with PowerShell.

## The script

# Set some variables to hold the source, name and destination of the update file
$UpdateUrl = "http://example.com/"$UpdateFile = "update.zip"
$Destination = "C:\Path\to\application\"$TempDir = $Env:Temp + "\" # Download the update file to a temporary directory$Client = New-Object System.Net.WebClient
$Client.DownloadFile($UpdateUrl + $UpdateFile,$TempDir + $UpdateFile) # Calculate MD5 hash of the downloaded update file$MD5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$Hash1 = [System.BitConverter]::ToString($MD5.ComputeHash([System.IO.File]::ReadAllBytes($TempDir +$UpdateFile))
)

# If an old update file exists at the destination, calculate its MD5 hash as well
If (Test-Path ($Destination +$UpdateFile)) {
$Hash2 = [System.BitConverter]::ToString($MD5.ComputeHash([System.IO.File]::ReadAllBytes($Destination +$UpdateFile))
)
} Else {
$Hash2 = "" } # Compare the MD5 hashes # If they're not equal, then copy the new update file to the destination and extract its contents If ($Hash1 -ne $Hash2) { Copy-Item ($TempDir + $UpdateFile)$Destination
$Shell = New-Object -ComObject Shell.Application$Shell.NameSpace($Destination).CopyHere($Shell.NameSpace($Destination +$UpdateFile).Items(),
20
)
}

Remove-Item ($TempDir +$UpdateFile)


## Explanation

The first block declares some variables that contain the name, source and destination of the update file, as well as a temporary directory to hold the downloaded update file. Don't forget the trailing slashes when you change them to your own paths.

The next block uses the WebClient object to download the file to the temporary directory.

Next, the script calculates the MD5 hash of the downloaded file. If you want to calculate a different hash, like SHA-1, check out the available classes in the System.Security.Cryptography namespace.

Then, the script checks for the existence of an old update file at the destination folder, and calculates its MD5 hash.

Next, the two hashes are compared. If they're not equal, it means there has been an update. The script then uses the Windows Shell object to copy the update file to the destination folder and extract its contents. The number 20 is the sum of two options of the CopyHere() function - 4 (which supresses the progress dialog) and 16 (which answers "Yes to All" to any dialogs, thus automatically overwriting existing files).

Finally, the last line deletes the downloaded update file from the temp directory.

## References

-
Does your code also extract the contents? Would it be possible to also compare existing "update.zip" inside the path with the one downloaded? – rationalboss Feb 18 '13 at 15:58
@rationalboss Yes, it extracts the contents. I added a quick explanation of the script to my post. How exactly do you want to compare the ZIP files - size, date modified, some kind of hash/checksum, something else? – Indrek Feb 18 '13 at 17:35
hash/checksum :) – rationalboss Feb 19 '13 at 4:08
@rationalboss Assuming MD5 is fine, I've updated the script in my post to compare the downloaded file to the existing update file in the destination directory, and only extract it if they're different. – Indrek Feb 20 '13 at 1:20

The trouble I see is that your client computers don't have access to your local folder so there's no way of them knowing it's time. If it's small, you could make it automatically download a new copy every so often anyway, or perhaps make a shell/bash script which they use to launch it, which checks for and downloads a new version each time they open up.

In case it's of use, if you're on Windows have a look at the RealtimeSync application which comes with FreeFileSync, you set it to monitor a folder for changes and then can have it execute a command when changes are discovered - such as a batch script to upload them for the other machines to download.

http://sourceforge.net/projects/freefilesync/

-
But the files are not accessible through NAS. It is accessible via http. Is that possible through FreeFileSync? – rationalboss Feb 17 '13 at 15:11
No, that's the trouble I see in the first paragraph. You'd have to download the update file before you'd know if it'd been updated. FFS could only upload the file for you once you make an edit, and even then only in conjunction with another script to do the uploading. – Patabugen Feb 19 '13 at 14:47