Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### Modify the following values as necessary
- $url = "https://files.fm/down.php?i=2xyfrrqq&n=test.zip"
- $outfile = "C:\tmp\test2.zip"
- $extractpath = "C:\tmp\extracted"
- $versionfile = "version.txt"
- $installpath = "C:\tmp\installed"
- $startfile = "C:\tmp\installed\application.exe"
- ####
- ### Leave these values as is
- $oldversion = "0.00"
- $newversion = "0.01"
- $arg = ("e "+$outfile+" -o"+$extractpath)
- function DownloadFile($url, $targetFile)
- {
- if (Test-Path $outfile) {
- remove-item $outfile -force
- }
- $timeA = get-date
- $uri = New-Object "System.Uri" "$url"
- $request = [System.Net.HttpWebRequest]::Create($uri)
- $request.set_Timeout(15000) #15 second timeout
- $response = $request.GetResponse()
- $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
- $responseStream = $response.GetResponseStream()
- $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
- $buffer = new-object byte[] 10KB
- $count = $responseStream.Read($buffer,0,$buffer.length)
- $downloadedBytes = $count
- while ($count -gt 0)
- {
- $targetStream.Write($buffer, 0, $count)
- $count = $responseStream.Read($buffer,0,$buffer.length)
- $downloadedBytes = $downloadedBytes + $count
- Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
- }
- $timeB = get-date ; $timeC = new-timespan $timeA $timeB ; $timeD = ($timeC.minutes)*60 + ($timeC.seconds)
- $filesize = (get-item $targetFile).length/1KB ; $avgspd = [math]::round(($filesize / $timeD),1)
- Write-Output "Downloaded '$($url.split('/') | Select -Last 1)' in $timeD seconds ($avgspd KB/s avg)"
- $targetStream.Flush() ; $targetStream.Close() ; $targetStream.Dispose() ; $responseStream.Dispose()
- }
- function RemoveLineCarriage($object)
- {
- $result = [System.String] $object;
- $result = $result -replace "`t","";
- $result = $result -replace "`n","";
- $result = $result -replace "`r","";
- $result = $result -replace " ;",";";
- $result = $result -replace "; ",";";
- $result = $result -replace [Environment]::NewLine, "";
- $result;
- }
- DownloadFile $url $outfile
- if (Test-Path $extractpath) {
- remove-item $extractpath -force -recurse
- }
- start-process "c:\program files\7-zip\7z.exe" $arg -wait
- remove-item $outfile -force ; write-output "Deleted file: $outfile"
- $oldversion = (get-content ($installpath + "\" + $versionfile) | out-string)
- $newversion = (get-content ($extractpath + "\" + $versionfile) | out-string)
- $oldversion = RemoveLineCarriage($oldversion)
- $newversion = RemoveLineCarriage($newversion)
- write-output ("Existing version is $oldversion | New version is: $newversion")
- if ($oldversion -eq $newversion) {
- write-output "Version match, retaining existing files..."
- if (Test-Path $extractpath) {
- remove-item $extractpath -force -recurse
- write-output "Extracted files have been deleted"
- }
- }
- else {
- write-output "Version mismatch"
- if (Test-Path $installpath) {
- remove-item $installpath -force -recurse
- write-output "Installed files have been deleted"
- }
- write-output "Moving files from $extractpath TO $installpath"
- move-item $extractpath $installpath -force
- }
- write-output (""+"Running: $startfile")
- start-process $startfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement