Results 1 to 18 of 18

Thread: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif

    Say there was a file on a web site:

    http://www.noteme.com/wokawidget/tickicon.gif

    For example.

    Is there any VB code that would allow me to find out it's size?
    Oh, the SystemFileObject doesn't do remote files, just thought I'd point that out


    Woka

  2. #2
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Not via HTTP as far as I'm aware, you'd have to use FTP "ways" or download the file via HTTP and check the size

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Hi Wokawidget, actually I have some experiance on this, so I know this for sure:

    Connect to the server with winsock, and send a request for "HEAD" header NOT "GET" header

    Here's the code, I already tested it
    I named the winsock "Sck"
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Sck.RemotePort = 80
    5.     Sck.RemoteHost = "www.waka.com"
    6.     Sck.Connect
    7. End Sub
    8.  
    9. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    10.     Sck.Close
    11. End Sub
    12.  
    13. Private Sub Sck_Connect()
    14.     Sck.SendData "HEAD /badger.exe HTTP/1.0" & vbNewLine & _
    15.                 "Host: www.woof.com" & vbNewLine & _
    16.                 "User-Agent: blah blah..." & vbNewLine & vbNewLine
    17. End Sub
    18.  
    19. Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
    20.     Dim sData As String, FromPos As Long, ToPos As Long
    21.     Sck.GetData sData
    22.    
    23.     FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
    24.     ToPos = InStr(FromPos + 1, sData, vbNewLine)
    25.    
    26.     MsgBox "The file size is: " & Mid(sData, FromPos + 16, ToPos - (FromPos + 16)) & " bytes"
    27. End Sub

    PS: The result I got was 717 bytes...
    Last edited by CVMichael; Jan 7th, 2005 at 06:57 PM.

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Nice! I like
    However, you confused me with:
    Code:
    "User-Agent: blah blah..."
    Are you assuming I know what "Blah Blah" is, or doesn't it matter what I type here?

    Also, even though I like the code, and it's very simple, I was looking for something that wasn't asynchronous. However, if it is the only method, then I can code that into my app.

    Woka

  5. #5

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    The reason I am asking is because of this thread:

    http://www.vbforums.com/showthread.php?t=319226

    I have created a multi app auto updater.

    The one functionality I would like to have is to show the file size of the download before it starts downloading.
    Do you have any suggestions that would fit into my code in the other thread?

    Woka

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Quote Originally Posted by Wokawidget
    Nice! I like
    However, you confused me with:
    Code:
    "User-Agent: blah blah..."
    Are you assuming I know what "Blah Blah" is, or doesn't it matter what I type here?
    Woka
    It does not matter what you type there, that's the client name, and it can be any name, but some servers don't accept the header (return an error) if the information is missing.

    I downloaded and looked at your "multi app auto updater", but I don't understand why you coose to download with the "olelib.tlb". And also, you make the code so complicated, and it makes 2 extra DLLs on top of that... too many DLLs, too many files (classes & modules)...

    Generally I would like to make thinkgs simple and to the point, if I would do this, I would do it with Winsock, having the same result, and with aproximatelly the same ammount of code, much less files & one DLL or OCX (wich I would prefer more, for something like this)

  7. #7

  8. #8

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    All those classes make the code simpler.
    And they are DLL's for a reason. You can use the vbDownload and vbAutoUpdater DLL's in any of your apps. No replicating code. Perfect sense if you ask me.

    The olelib.tlb is my only draw back, but if you show me a way I can do this with winsock then that would please me...

    Woka

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Quote Originally Posted by Wokawidget
    The olelib.tlb is my only draw back, but if you show me a way I can do this with winsock then that would please me...
    Woka
    Well, actually it's quite simple...
    Change the header for the code I pasted earlier, from "HEAD..." to "GET..."
    That way, the server will send the header (same as when you send the HEAD command"), then 2 new lines, and then the file data.
    So, in the first DataArrival event look for the 2 new lines, anything before that, is the header that you want to parse to get the file size (or any other info that might be usefull about the server...), and anything after the 2 new lines, save in the file... this way, you can also make a progress bar, because you get the file size first, then the file data...

    If you want, I can make a short example of what I just mentioned.

  10. #10

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    If you wouldn't mind. That would be great.
    Wonder what the performance difference would be.

    Woka

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Here's the modified code.
    It downloads the file in the App.Path directory
    VB Code:
    1. Option Explicit
    2.  
    3. Private FileLength As Long, FileNum As Integer
    4.  
    5. Private Sub Form_Load()
    6.     FileNum = FreeFile
    7.     Open App.Path & "\badger.exe" For Binary Access Write Lock Write As FileNum
    8.    
    9.     Sck.RemotePort = 80
    10.     Sck.RemoteHost = "www.waka.com"
    11.     Sck.Connect
    12. End Sub
    13.  
    14. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    15.     Sck.Close
    16. End Sub
    17.  
    18. Private Sub Sck_Connect()
    19.     Sck.SendData "GET /badger.exe HTTP/1.0" & vbNewLine & _
    20.                 "Host: www.woof.com" & vbNewLine & _
    21.                 "User-Agent: blah blah..." & vbNewLine & vbNewLine
    22. End Sub
    23.  
    24. Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
    25.     Dim sData As String, FromPos As Long, ToPos As Long
    26.     Sck.GetData sData
    27.    
    28.     If FileLength = 0 Then
    29.         Debug.Print sData
    30.        
    31.         If sData Like "HTTP/1.# 200*" Then
    32.             FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
    33.             ToPos = InStr(FromPos + 1, sData, vbNewLine)
    34.            
    35.             FileLength = Val(Mid(sData, FromPos + 16, ToPos - (FromPos + 16)))
    36.             ToPos = InStr(1, sData, vbNewLine & vbNewLine)
    37.            
    38.             sData = Mid(sData, ToPos + 4)
    39.         Else
    40.             ' some kind of error from the server, check the header (usually file not found)
    41.         End If
    42.     End If
    43.    
    44.     If FileLength > 0 And Len(sData) > 0 Then
    45.         Put FileNum, , sData
    46.        
    47.         If LOF(FileNum) >= FileLength Then
    48.             MsgBox "DONE"
    49.             Close FileNum
    50.             FileNum = 0
    51.             FileLength = 0
    52.         End If
    53.     End If
    54. End Sub

  12. #12

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Nice code.
    But out of interest, what's wrong with DownloadURL API that I was using?
    It does everything that the winsock does, but automatically. Also, it comes with other benefits, like caching the file.
    So, you have to include 2 tlb files in your app...and? When dealing with large apps that are very complex then you will have many many many DLLs associated with your app. Some your have written yourself, and some that are 3rd party.
    Splitting an app up into different components, whether that be internal classes, or DLL, benefits you and the development of the application.
    There is no way everything should be placed in one exe. This makes for bad app design.

    Woka

  13. #13

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Am trying to get your code working, I have:
    Code:
    Public Sub GetFileInformation()
    Dim strData     As String
    On Error GoTo ErrHandler
        mlngDownloadType = TYPE_INFO
        If IsConnected Then
            strData = "HEAD " & mstrRemoteFile & " HTTP/1.0" & vbNewLine
            strData = strData & "Host: www.Woka.com" & vbNewLine
            strData = strData & "User-Agent: blah blah..." & vbNewLine & vbNewLine
            mobjSocket.SendData strData
        Else
            Connect
        End If
        Exit Sub
    ErrHandler:
        Disconnect
        Err.Raise Err.Number, Err.Source, Err.Description
    End Sub
    Where:
    Code:
    mstrRemoteFile = "/Wokawidget/TickIcon.gif"
    and
    Code:
    mobjSocket.RemoteHost = "www.noteme.com"
    mobjSocket.RemotePort = 80
    but I always get "404 File Not Found" as the returned data.
    It exists, use IE to browse to it. NoteMe is hosting it for me.

    The sent data was:
    HEAD /wokawidget/TickIcon.gif HTTP/1.0
    Host: www.Woka.com
    User-Agent: blah blah...
    and the returned data is:
    HTTP/1.1 404 Not Found
    Date: Wed, 19 Jan 2005 02:15:14 GMT
    Server: Apache/1.3.33 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.10 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a
    Connection: close
    Content-Type: text/html; charset=iso-8859-1
    Does it have anything to do with HTTP/1.1 and HTTP/1.0.
    Also, "Connection: close", does this mean there is no connection?
    The GetFileInformation function was fired on the OnConnect event from winsock.

    Any ideas why it can't see it?

    Woka

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.woof.com/badger.exe

    Are you sure the path is correct ?
    Maybe:
    mstrRemoteFile = "/TickIcon.gif" ?

    If you type the link in IE, what is the COMPLETE link ? http://... ?

    Also, "Connection: close", does this mean there is no connection?
    It basically means that the server will close the connection once it's done sending all data. Some servers keep the connection open because you might want to send another request, and so on...

  15. #15

  16. #16
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif

    I colored in Red the changes you have to make.
    This code worked for me...
    VB Code:
    1. Option Explicit
    2.  
    3. Private FileLength As Long, FileNum As Integer
    4.  
    5. Private Sub Form_Load()
    6.     FileNum = FreeFile
    7.     Open App.Path & [COLOR=Red]"\TickIcon.gif"[/COLOR] For Binary Access Write Lock Write As FileNum
    8.    
    9.     Sck.RemotePort = 80
    10.     Sck.RemoteHost = [COLOR=Red]"www.noteme.com"[/COLOR]
    11.     Sck.Connect
    12. End Sub
    13.  
    14. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    15.     Sck.Close
    16. End Sub
    17.  
    18. Private Sub Sck_Connect()
    19.     Sck.SendData "GET [COLOR=Red]/Wokawidget/TickIcon.gif[/COLOR] HTTP/1.0" & vbNewLine & _
    20.                 "Host: [COLOR=Red]www.noteme.com"[/COLOR] & vbNewLine & _
    21.                 "User-Agent: blah blah..." & vbNewLine & vbNewLine
    22. End Sub
    23.  
    24. Private Sub Sck_DataArrival(ByVal bytesTotal As Long)
    25.     Dim sData As String, FromPos As Long, ToPos As Long
    26.     Sck.GetData sData
    27.    
    28.     If FileLength = 0 Then
    29.         Debug.Print sData
    30.        
    31.         If sData Like "HTTP/1.# 200*" Then
    32.             FromPos = InStr(1, sData, "Content-Length:", vbTextCompare)
    33.             ToPos = InStr(FromPos + 1, sData, vbNewLine)
    34.            
    35.             FileLength = Val(Mid(sData, FromPos + 16, ToPos - (FromPos + 16)))
    36.             ToPos = InStr(1, sData, vbNewLine & vbNewLine)
    37.            
    38.             sData = Mid(sData, ToPos + 4)
    39.         Else
    40.             ' some kind of error from the server, check the header (usually file not found)
    41.         End If
    42.     End If
    43.    
    44.     If FileLength > 0 And Len(sData) > 0 Then
    45.         Put FileNum, , sData
    46.        
    47.         If LOF(FileNum) >= FileLength Then
    48.             MsgBox "DONE"
    49.             Close FileNum
    50.             FileNum = 0
    51.             FileLength = 0
    52.         End If
    53.     End If
    54. End Sub

  17. #17

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif

    Think I just realised what was going wrong

    The following works:

    /Wokawidget/TickIcon.gif

    This doesn't:

    /Wokawidget/tickicon.gif

    Stupid apache web server with case sensitive settings

    Cheers for your help.

    Woka

  18. #18

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Find file size of remote file ie http://www.noteme.com/wokawidget/tickicon.gif

    Balls!
    Not only is it case sensetive, but:
    Code:
     "Host: www.noteme.com" & vbNewLine & _
    That MUST be the host remote server iun there.
    I had www.woof.com, which is another reason it failed

    Got it working now...WOOOHOOOOOOOOOOO!
    Thank you.

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width