Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Download Progress Bar


  • Please log in to reply
26 replies to this topic
Kerry
  • Members
  • 144 posts
  • Last active: Sep 25 2006 07:33 PM
  • Joined: 20 Jul 2006
This is functions will download and put up a progress bar for your download. This may have been done before, but I couldn't find a clean example of it with a progress bar (there were other progress methods that were used - some for tranfering files, some for downloading)

Thanks to Rajat for his example and Gre and Ranomore for their example

;dLocation = URL of download, path = where to download to and what name
Download(path, dLocation, FullFileName)
{
	Progress, H80, , Downloading..., %FullFileName% Download
	SetTimer, GetSize, 1000	
	UrlDownloadToFile, %dLocation%, %path%
	Progress, Off
	SetTimer, GetSize, Off
	Return
}

GetSize:
	FullSize := HttpQueryInfo(dLocation, 5) ; get download file size in bytes
	FileGetSize, FSize, %path%, K ; Get file size in kb
	FullSize := FullSize / 1000 ; Make in to kb
	UpdateSize := FSize / FullSize ; get percentage
	UpdateSize := UpdateSize * 100 ; take out of decimal form
	UpdateSize := Floor(UpdateSize)
	IfEqual, FSize, FullSize, Return
	IfNotEqual, ErrorLevel, 1
		Progress, %UpdateSize%, %UpdateSize%`% Complete, Downloading..., %FullFileName% Download - %UpdateSize%`% Complete
Return

Note: FullFileName, Path and dLocation need to be declared earlier in the script for GetSize: to pick up
where:
dLocation is the URL of the downloaded file
FullFileName can be anything, I name it the full name of the file (for example, "TUGZip 3.4")
path is the location you want the file to be downloaded to on the disk (which includes the file name, could be %A_ScriptDir%\tugzip34.exe)

Also, HttpQueryInfo is used, which can be found here

EDIT: There was a slight error where the Progress window didn't close. That's corrected. Also made the timer interval less.

-Kerry

Other keywords: status box

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for posting this. It's good to see all the info summarized in one place so concisely.

Fuco
  • Members
  • 49 posts
  • Last active: Oct 24 2008 06:21 PM
  • Joined: 21 Mar 2006
hmm, this is nice but have some flaws.
1) i cant call function like this
Download("e:\dk\dotakeys.exe", "http://www.longmark.sk/access/dotakeys1.3/dotakeys.exe", "dotakeys.exe") and this is really bad about it :( ( but it can be fixed very easy )

2) it dont work with smaller files ( like <500 kb or so ). It only shows me 0% and then disappear. ( and i dont have so fast conection it can be dl-ed in 1 sec :) ) it stay there on 0% for 8 sec and dont update.
RegExReplace("C:\Program Files\AutoHotkey", "(^C)(?=\W).{4}((?i)[GOD])\w{1,4}\s(\D)(?:\w+)*\\(?3)(u|o).*?(k).*" , "$3$4$l1$5$2")

Fuco
  • Members
  • 49 posts
  • Last active: Oct 24 2008 06:21 PM
  • Joined: 21 Mar 2006
now i modified it a bit and u can call it with download(this, from ther, name) and donot need to set them before

Download(path_p, dLocation_p, FullFileName_p)
{
   global path, dLocation, FullFileName,   ;NEW
   path = %path_p%                             ;NEW
   dLocation = %dLocation_p%               ;NEW
   FullFileName = %FullFileName_p%      ;NEW  
   Progress, H80, , Downloading..., %FullFileName% Download
   SetTimer, GetSize, 500
   UrlDownloadToFile, %dLocation%, %path%
   Progress, Off
   SetTimer, GetSize, Off
   Progress, Off
   Return
}

RegExReplace("C:\Program Files\AutoHotkey", "(^C)(?=\W).{4}((?i)[GOD])\w{1,4}\s(\D)(?:\w+)*\\(?3)(u|o).*?(k).*" , "$3$4$l1$5$2")

Kerry
  • Members
  • 144 posts
  • Last active: Sep 25 2006 07:33 PM
  • Joined: 20 Jul 2006
Thank you for those notes and corrections.

I also noted you put a Progress, Off after the SetTimer, GetSize, Off, is there any reason for this?

As far as the progress bar not working on that... I think its under 1mb because I had a similar problem, but I figured that it was just because the download hadn't actually installed (it lasted for like 2 seconds) and I figured it connection just dl'ed it that fast. I will look into it.

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
This is a very useful technique, I'll be using it a lot. I modified your function a little bit so everything is done within the function:
Download(url, save, msg = 0x1100, sleep = 250) {
	total := HttpQueryInfo(url, 5)
	SetTimer, _dlprocess, %sleep%
	UrlDownloadToFile, %url%, %save%
	SetTimer, _dlprocess, Off
	Return, ErrorLevel
	_dlprocess:
	FileGetSize, current, %save%, K
	Process, Exist
	PostMessage, msg, current * 1024, total, , ahk_pid %ErrorLevel%
	Exit
}
url: the URL of the file to download
save: where the downloaded file should be saved to
msg: [0x1100] the message the script should send itself (see below)
sleep: [250] how often to send the message in ms

Download can be monitored with OnMessage(), wParam is the current progress of the download and lParam is the total size of the remote file, both in bytes. Here is an uncommented GUI script example (you'll need to include HttpQueryInfo and the above function):
url = http://dl.google.com/earth/GoogleEarth.exe
save = %A_Temp%\earth.exe
FileDelete, %save%
message = 0x1100

Gui, Add, Progress, Section vProgressBar w500 -Smooth
Gui, Font, bold
Gui, Add, Text, vProgressN ys, ..........
Gui, Add, Text, xm Section, Downloading:
Gui, Font
Gui, Add, Text, ys, %url%
Gui, Add, Text, vKB ys w200

Gui, Show, , Downloading...
OnMessage(message, "SetCounter")
Download(url, save, message, 50)
GuiControl, , ProgressBar, 100
GuiControl, , ProgressN, 100`%
GuiControl, , KB
Gui, Show, , Downloaded
Return

GuiEscape:
GuiClose:
ExitApp

SetCounter(wParam, lParam) {
	progress := Round(wParam / lParam * 100)
	GuiControl, , ProgressBar, %progress%
	GuiControl, , ProgressN, %progress%`%
	wParam := wParam // 1024
	lParam := lParam // 1024
	GuiControl, , KB, (%wParam%kb of %lParam%kb)
	Gui, Show, , %progress%`% - Downloading...
}

Ofcourse if you prefer the simpler Progress instead of a GUI:
url = http://dl.google.com/earth/GoogleEarth.exe
save = %A_Temp%\earth.exe
FileDelete, %save%
message = 0x1100
Progress, M h80 w500, , .
OnMessage(message, "SetCounter")
Download(url, save, message, 50)
ExitApp

SetCounter(wParam, lParam) {
	global url
	progress := Round(wParam / lParam * 100)
	wParam := wParam // 1024
	lParam := lParam // 1024
	Progress, %progress%, %url%, Downloading %wParam%kb of %lParam%kb..., %progress%`% - Downloading...
}

Thanks again :)

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Kerry
  • Members
  • 144 posts
  • Last active: Sep 25 2006 07:33 PM
  • Joined: 20 Jul 2006
Hi Titan, thanks!

Didn't realize this has been updated. I was wondering, though, because I like to make functions inclusive (so no other code has to be placed elsewhere). Would you be able to do that with your code?

I've made a mod of HttpQueryInfo that works as a sub. It can be found here.

I'm a bit tired of recently and you're a better programmer than I... and I'm not sure if I could make a working version of your code. I also know that while using a function you can't make a gui with global variables, and that's what I ran into before when I tried to do it, and I'm not sure how to work around it or I would have tried :)

-Kerry

webPragmatist
  • Members
  • 33 posts
  • Last active: Dec 27 2007 09:35 PM
  • Joined: 18 May 2007
What's a decent way to incorporate creating the GUI, updating the GUI, and downloading the item all in a few functions...

All I want is to be able to input the URL and it would create a standardized download progress dialog.

The reason I ask is that this is a mere portion of what my script does all the download GUI would do is download an update.

I have been messing around with everything and I can't seem to figure it out :?

Can someone lend me a hand?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

All I want is to be able to input the URL and it would create a standardized download progress dialog.


You may try this: <!-- m -->http://www.autohotke... ... 074#118074<!-- m -->

:roll:

WhoMe
  • Guests
  • Last active:
  • Joined: --
Can someone please show an example on how this can be implemented?

  • Guests
  • Last active:
  • Joined: --
Put that & HttpQueryInfo() in a file, use like this:
Download("C:\google.html","http://www.google.com","google.html")


bixtool
  • Members
  • 27 posts
  • Last active: Nov 09 2008 05:12 AM
  • Joined: 16 Aug 2008
Titan,
I've incorporated your code in my script. The files are downloaded. The progress bar works but the text indicating the file downloading (url) is missing.
Progress, %progress%, %url%, Downloading %wParam%kb of %lParam%kb..., %progress%`% - Downloading...

So, the SubText is missing from my progress control. I check in
SetCounter(wParam, lParam) {
   global url

url == blank

Basically, I added both functions you gave and added the following where I needed to download a file:
save = Banner.jpg
message = 0x1100
Progress, M h80 w500, , . 
OnMessage(message, "SetCounter") 
Download(url, save, message, 50)

Any thoughts on my issue?

bixtool
  • Members
  • 27 posts
  • Last active: Nov 09 2008 05:12 AM
  • Joined: 16 Aug 2008

Titan,

SetCounter(wParam, lParam) {
   global url

url == blank


Sorry, ignore this request. I saw my answer after I posted a snipped of your code.

I had to declare url as a global variable in all my procedures/functions.

BTW: great script. Thanks for posting

bixtool
  • Members
  • 27 posts
  • Last active: Nov 09 2008 05:12 AM
  • Joined: 16 Aug 2008
I'm using titan's functions to download several file types. I've noticed that the file size for html files is being reported as zero.
ex.
Downloading 28Kb of 0kb

Could ths be my mistake?

Chavez
  • Members
  • 256 posts
  • Last active: Oct 13 2009 01:27 PM
  • Joined: 20 Aug 2008
Oh very nice, with this you could make a downloader for a specific program or game (like I've seen in 9Dragons).

Thanks again!
-Chavez.