URLDownloadtoFile

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
lehenryjr
Posts: 5
Joined: 04 Feb 2015, 11:05

URLDownloadtoFile

04 Feb 2015, 11:23

I wrote this update script for a program that doesn't auto-update. It downloads the latest USB version that can be extracted and then the files copied into place. It used to work and now seems to have stopped.

The problem seems to be that the URLDownloadtoFile function isn't working properly. In my testing, it seems the download is being launched, but the script isn't waiting for the file to complete the download. The script continues and pretty much fails everything afterwards.

mnkdir is the folder where the program resides.

Here's the heart of the script I have:

Code: Select all

FileCreateDir, %mnkdir%\tempfiles
UrlDownloadToFile, http://mynoteskeeper.com/mnk.zip, %mnkdir%\tempfiles\mnk.zip
sleep, 1000
;msgbox,Pause

traytip, %appname%, Extracting new files…
run, %mnkdir%\tempfiles\unzip.EXE -o %mnkdir%\tempfiles\mnk.zip -d %mnkdir%\tempfiles 
sleep, 2000
;msgbox,Pause
Has anyone seen this problem before, or is there a better way to do this?
I would love something with a progress bar, but I'm not that good [yet].

Thanks in advance...
expert_vision
Posts: 21
Joined: 13 Jan 2014, 10:47

Re: URLDownloadtoFile

04 Feb 2015, 12:35

It fails because "%mnkdir%\tempfiles" path doesn't exist. Make sure it exist before downloading.
lehenryjr
Posts: 5
Joined: 04 Feb 2015, 11:05

Re: URLDownloadtoFile

04 Feb 2015, 12:46

Thanks for your reply.
I've been testing further.

It doesn't like the %mnkdir% variable at all. I did make an effort to combine the entire path with the destination file into one variable. The script acted like it was downloading the file [with a long enough pause] but once the script was over, file never [MNK.zip] appeared. Odd.

The path is valid and I have checked it several times.

If I use this, it works:

Code: Select all

UrlDownloadToFile, http://mynoteskeeper.com/mnk.zip, Mnk.zip
If I use this, it doesn't work:

Code: Select all

UrlDownloadToFile, http://mynoteskeeper.com/mnk.zip, %mnkdir%\tempfiles\MNK.zip
I verified the path is correct and I have verified that the folders have been created. I have been using this script to update the program for the last two years. It seems very odd it just stops working.

If I have to download the files to the same directory where the script is, that's fine, but I was kind of hoping I can just download the files directly to the path that I wanted [and have been using].

I appreciate your help.
expert_vision
Posts: 21
Joined: 13 Jan 2014, 10:47

Re: URLDownloadtoFile

04 Feb 2015, 15:03

Have you checked the path in the script is actually correct?

Code: Select all

MsgBox ,"%mnkdir%\tempfiles\MNK.zip"
IfNotExist, %mnkdir%\tempfiles\
	MsgBox, "%mnkdir%\tempfiles\" does not exist.
UrlDownloadToFile, http://mynoteskeeper.com/mnk.zip, %mnkdir%\tempfiles\MNK.zip
Is %mnkdir% ending in a backslash ("\") ? If it is, a path such as "C:\Program Files\MNK\\tempfiles\MNK.zip" is not valid because of the double backslash.

Also, if "%mnkdir%\tempfiles\MNK.zip" already exists, make sure it doesn't have same weird attribute like read-only or archive.

Other than that, I have no idea. File/folder security permissions ?
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: URLDownloadtoFile

04 Feb 2015, 16:25

URLDownloadToFile calls InternetOpenUrl (which probably connects to the server and initiates the request) before attempting to open the file for writing, hence the delay even if it can't write the file.
expert_vision wrote:If it is, a path such as "C:\Program Files\MNK\\tempfiles\MNK.zip" is not valid because of the double backslash.
File dialogs will reject double slashes (and other valid but not perfectly formed paths), but most other things accept them. For instance, this works:

Code: Select all

URLDownloadToFile http://ahkscript.org/download/1.1/version.txt
 , %A_ScriptDir%\\\\\version.txt
I suppose that either the path is incorrect (in some other way) or the script doesn't have permission to write to that file. Does this succeed?

Code: Select all

FileCreateDir %mnkdir%\tempfiles
if ErrorLevel
    throw A_LastError
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: URLDownloadtoFile

04 Feb 2015, 17:47

maybe try with wget
script downloads small program wget.exe if not exists and click start
starts download http://mynoteskeeper.com/mnk.zip to desktop\test

Code: Select all

modified=20140803
;-http://eternallybored.org/misc/wget/wget.exe
;-a wget example
;-http://ahkscript.org/boards/viewtopic.php?f=5&t=4096

url1=http://eternallybored.org/misc/wget/wget.exe
WGET=%A_SCRIPTDIR%\wget.exe
ifnotexist,%wget%
  urldownloadtofile,%url1%,%wget%

Loop %WGET%,1
   SP1= %A_LoopFileShortPath%    ;- shortpath for DOS command

;C4:="http://www.radiogeorge.com/oldies/oldies2/Guy%20Mitchell-Singing%20The%20Blues.mp3"
C4:="http://mynoteskeeper.com/mnk.zip"

R3M=%a_desktop%\TEST
ifnotexist,%R3M%
   FileCreateDir,%R3M%

SplitPath,c4,name, dir, ext, name_no_ext, drive
stringreplace,name,name,`%20,%a_space%,all
fx=%r3m%\%name%

Gui,1:Font,,FixedSys
Gui,1:Add,Listbox, x10  y5    h190  w680 vLB1,
Gui,1:Add,button,  x20  y200  h20   w100 gSTART vStart1,START
GuiControl,1:,LB1,Click Start to download with WGET=|%c4%
Gui,1:Show, x0 y0 h225 w700 , Wget_Test
return
;-------------------------------------------------------

START:
Gui,1:submit,nohide
ifexist,logfile.txt
  filedelete,logfile.txt
GuiControl,1: Disable,start1
GuiControl,1:,LB1,|
GuiControl,1:,LB1,Wait `, WGET is running=|%c4%

aaa= -U --tries=1 --directory-prefix="%R3M%" -o logfile.txt
;runwait,%COMSPEC% /k %sp1% "%C4%" %aaa%        ;- << see what happens
runwait,%COMSPEC% /c %sp1% "%C4%" %aaa%,,hide   ;- hidden

Loop,read,logfile.txt
   GuiControl,1:,LB1,%A_LoopReadLine%
;ifexist,%fx%
;   run,%fx%
return
;--------------------------------------------------------

Guiclose:
exitapp
;========================================================
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: URLDownloadtoFile

04 Feb 2015, 19:35

Also check out this thread: http://ahkscript.org/boards/viewtopic.php?f=6&t=1674 for some file download functions
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: URLDownloadtoFile

05 Feb 2015, 16:11

If I have to download the files to the same directory where the script is, that's fine, but I was kind of hoping I can just download the files directly to the path that I wanted [and have been using].
You could "cheat" and set the working directory to the target folder. Then just download to filename. I hate giving up on stuff myself. But sometimes it can take years to find out why something that should work won't. ;)
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Fredrik F, maxkill, RandomBoy, ShatterCoder and 399 guests