Jump to content

How to get File name from link


storme
 Share

Recommended Posts

G'day all

This is annoying me at the moment as I can't find a solution.

I'm trying to download updates from a site (driverpacks.net).

To get the torrent files you click on a link that looks like

http://driverpacks.net/driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent

yet the file downloaded is

http://driverpacks.net/driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent/DP_MassStorage_wnt5_x86-32_1011.torrent

I'd like to download using their name but the closest I can come is

Local $hDownload = InetGet("http://driverpacks.net/driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent",@ScriptDir & "\test.torrent")
If @error Then
    MsgBox(0, "ERROR:", "Download Failed")
EndIf

I'd like to get the REAL filename so I can compare it agsainst existing files so I don't redownload files I already have.

Thanks for any help you can offer.

John Morrison

aka

Storm-E

Link to comment
Share on other sites

This won't download the file for you, you'll have to write that code. But this will identify the file name for you. You could run something like this before downloading the file to check to see if you already had it:

Dim $lArray
$URL = "http://driverpacks.net/driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent/DP_MassStorage_wnt5_x86-32_1011.torrent"

$lArray = StringSplit($URL, "/")
$File_Name = $lArray[UBound($lArray)-1]

MsgBox(0,"File Name Is...",$File_Name)

That identified the file for me.

-Fett

[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
Share on other sites

You misunderstood what he wanted fett8802.

To get the filename of a redirect, you'll have to read the HTTP stream manually or use a library that handles that. It may be a redirect and it may also load as an attachment.

If it's an attachment, you'd read the name in the response header that says 'Content-Disposition: attachment;filename="nameoffile"'. The nameoffile part is the part you are wanting to read.

Link to comment
Share on other sites

You can use WinHttp.au3 for that what Richard Robertson suggested:

#include "WinHttp.au3"

; Open needed handles
Global $hOpen = _WinHttpOpen()
Global $hConnect = _WinHttpConnect($hOpen, "driverpacks.net")
; Specify the reguest:
Global $hRequest = _WinHttpOpenRequest($hConnect, Default, "driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent")

; Send request
_WinHttpSendRequest($hRequest)

; Wait for the response
_WinHttpReceiveResponse($hRequest)

Global $sHeader
 ; If there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_DISPOSITION)
;Or maybe:
;$sHeader = _WinHttpQueryHeaders($hRequest, BitOR($WINHTTP_QUERY_RAW_HEADERS_CRLF, $WINHTTP_QUERY_CUSTOM), "Content-Disposition")

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)


; Display retrieved header
ConsoleWrite($sHeader & @CRLF)
MsgBox(0, "Header", $sHeader)
Edited by trancexx
Link to comment
Share on other sites

You can use WinHttp.au3 for that what Richard Robertson suggested:

<SNIP>

Thanks to BOTH of you!

That code is exactly what I was looking for and works perfectly.. :x

Now I can use a simple FileExists check instead of extracting version numbers from the url and a lot of spaghetti code.

THANKS!

Link to comment
Share on other sites

Without WinHTTP UDF.

ConsoleWrite(_HTTPGetResponse('http://driverpacks.net/driverpacks/windows/xp/x86/mass-storage/10.11/download/torrent') & @CR)

Func _HTTPGetResponse($sUrl)

    Local $Error, $Socket, $Recv = ''
    Local $Host = StringRegExpReplace($sUrl, '.*://(.*?)/.*', '\1')
    Local $Page = StringRegExpReplace($sUrl, '.*://.*?(/.*)', '\1')
    Local $Send = 'HEAD ' & $Page & ' HTTP/1.1' & @CRLF & _
                  'Host: ' & $Host & @CRLF & _
                  'User-Agent: AutoIt/' & @AutoItVersion & ' (Windows; U; Windows NT 5.1; en-US; rv:1.8.1)' & @CRLF & _
                  'Referer: ' & $Host & @CRLF & _
                  'Connection: close' & @CRLF & @CRLF

    TCPStartup()
    If @error Then
        Return SetError(@error, 0, '')
    EndIf
    $IP = TCPNameToIP($Host)
    If @error Then
        Return SetError(@error, TCPShutdown(), '')
    EndIf
    $Socket = TCPConnect($IP, 80)
    If @error Then
        Return SetError(@error, TCPShutdown(), '')
    EndIf
    If Not TCPSend($Socket, $Send) Then
        Return SetError(@error, TCPCloseSocket($Socket) + TCPShutdown(), '')
    EndIf
    Do
        $Recv &= TCPRecv($Socket, 16)
    Until @error
    If Not $Recv Then
        $Error = @error
    Else
        $Error = 0
    EndIf
    TCPCloseSocket($Socket)
    TCPShutdown()
    Return SetError($Error, 2, $Recv)
EndFunc   ;==>_HTTPGetResponse
Link to comment
Share on other sites

Without WinHTTP UDF.

Thanks Yashied that will do perfectly for my app.

WinHttp.au3 is more flexable BUT as this is the only thing I need (I hope) I like the slimer code.

THANKS!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...