how to get the original file name from UrlDownloadToFile? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wer
Posts: 57
Joined: 29 Nov 2022, 21:28

how to get the original file name from UrlDownloadToFile?

Post by wer » 05 Apr 2023, 03:06

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

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to get the original file name from UrlDownloadToFile?

Post by mikeyww » 05 Apr 2023, 04:45

Hello,

If you have the URL, you can use :arrow: SplitPath to get the file name.

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: how to get the original file name from UrlDownloadToFile?

Post by garry » 05 Apr 2023, 15:02

as @mikeyww mentioned
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
}
;===================================================

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to get the original file name from UrlDownloadToFile?

Post by wer » 06 Apr 2023, 05:36

@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

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to get the original file name from UrlDownloadToFile?

Post by wer » 06 Apr 2023, 05:39

@mikeyww
dear mr.mikey, is there any possible way without reading url itself?

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: how to get the original file name from UrlDownloadToFile?  Topic is solved

Post by malcev » 06 Apr 2023, 06:34

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()

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to get the original file name from UrlDownloadToFile?

Post by wer » 06 Apr 2023, 06:44

@malcev
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.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: how to get the original file name from UrlDownloadToFile?

Post by malcev » 06 Apr 2023, 06:51

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

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to get the original file name from UrlDownloadToFile?

Post by wer » 06 Apr 2023, 06:53

malcev wrote:
06 Apr 2023, 06:51
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
thanks a lot for help sir
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.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: how to get the original file name from UrlDownloadToFile?

Post by malcev » 06 Apr 2023, 06:55

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

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to get the original file name from UrlDownloadToFile?

Post by wer » 06 Apr 2023, 07:58

@malcev
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
    ...
}

Post Reply

Return to “Ask for Help (v1)”