how to get the original file name from UrlDownloadToFile? Topic is solved
how to get the original file name from UrlDownloadToFile?
sometimes download url is not include the file name,
and dont want to specify the name before download,
so is there possible to get the downloaded file name?
thanks for help in advance
and dont want to specify the name before download,
so is there possible to get the downloaded file name?
thanks for help in advance
Re: how to get the original file name from UrlDownloadToFile?
as @mikeyww mentioned
example below removes %20 etc in filename
;-
example below removes %20 etc in filename
;-
Code: Select all
url:="https://brisbane2.hopto.org/shared-brisbane2/music_fh/Hank%20Thompson%20--%20The%20Wild%20Side%20Of%20Life.mp3"
;-https://www.autohotkey.com/docs/commands/URLDownloadToFile.htm
decoded:= uriDecode(url) ;- remove %20 etc
SplitPath,decoded, name, dir, ext, name_no_ext, drive
File :=a_desktop . "\" . name
ifnotexist,%file%
{
SplashImage,,M b fs12 ctFFFF00 cwBlack x100 y50 ZW800,Download=`n%name%
start := A_TickCount
urldownloadtofile,%url%,%file%
aa:=A_TickCount - start
Splashimage,off
msgbox, 262208, ,FILE = %file%`nis downloaded in = %aa%-ms,2 ;- show for 2 seconds and then play
}
try
run,%file%
return
;--------------------------------------------------
uriDecode(str) {
Loop
If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
Else Break
Return, str
}
;===================================================
Re: how to get the original file name from UrlDownloadToFile?
@garry
right, thank you sir, its a good example, but
how to deal someting like this:
https://go.microsoft.com/fwlink/?linkid=2186537
even no file name inside
right, thank you sir, its a good example, but
how to deal someting like this:
https://go.microsoft.com/fwlink/?linkid=2186537
even no file name inside
Re: how to get the original file name from UrlDownloadToFile?
@mikeyww
dear mr.mikey, is there any possible way without reading url itself?
dear mr.mikey, is there any possible way without reading url itself?
Re: how to get the original file name from UrlDownloadToFile? Topic is solved
Code: Select all
url := "https://go.microsoft.com/fwlink/?linkid=2186537"
HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
HTTP.Option(6) := 0 ; disable redirects
HTTP.Open("GET", url)
HTTP.Send()
MsgBox, % HTTP.GetAllResponseHeaders()
HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
HTTP.Open("GET", url)
HTTP.SetRequestHeader("Range", "bytes=0-0") ; download 1 byte
HTTP.Send()
MsgBox, % HTTP.GetAllResponseHeaders()
Re: how to get the original file name from UrlDownloadToFile?
@malcev
wow, you are real pro
so which is better sir?
method 2 seems faster than 1
wow, you are real pro
so which is better sir?
method 2 seems faster than 1
Last edited by wer on 06 Apr 2023, 06:53, edited 2 times in total.
Re: how to get the original file name from UrlDownloadToFile?
I did not compare them, but to get filename from links like this We need start download.
https://drive.google.com/uc?id=1RG3CAPiwiFlFATUlIIwhk0RrbEU4PgVP&export=download
https://drive.google.com/uc?id=1RG3CAPiwiFlFATUlIIwhk0RrbEU4PgVP&export=download
Re: how to get the original file name from UrlDownloadToFile?
thanks a lot for help sirmalcev wrote: ↑06 Apr 2023, 06:51I did not compare them, but to get filename from links like this We need start download.
https://drive.google.com/uc?id=1RG3CAPiwiFlFATUlIIwhk0RrbEU4PgVP&export=download
still want to know how to catch the error if url is unavailable
do not want script suspend when error occurred
Last edited by wer on 06 Apr 2023, 06:56, edited 1 time in total.
Re: how to get the original file name from UrlDownloadToFile?
Code: Select all
url := "https://drive.google.com/uc?id=1RG3CAPiwiFlFATUl&export=download"
HTTP := ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
HTTP.Open("GET", url)
HTTP.Send()
MsgBox, % HTTP.Status
Re: how to get the original file name from UrlDownloadToFile?
@malcev
k i think i got it thank you sir
k i think i got it thank you sir
Code: Select all
get_origin_name(url) {
get_retry_time:=1
loop, {
try
{
whr:=ComObjCreate("WinHTTP.WinHTTPRequest.5.1")
whr.Open("GET", url)
whr.SetRequestHeader("Range", "bytes=0-0")
whr.Send()
String:=whr.GetAllResponseHeaders()
Array :=StrSplit(String, "filename=")
file_name:=StrReplace(Array[2], """")
file_name:=StrReplace(file_name, "`r`n")
}
if (file_name!="") {
get_fail:=0
break
} else if (get_retry_time=retry_limit) {
get_fail:=1
break
}
get_retry_time++
}
if (get_fail=1) {
...
}
if get_fail=0
...
}