Checking size of file on web server...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I trying to write a script to compare filesizes of local and remote (on a web server) files...

I can get the local filesize by using:


Code:
Dim localFileSize As Long
localFileSize = FileLen(ThisWorkbook.Path  & Filename)

MsgBox "local - " & localFileSize

I can check if my 'remote' file exists by using:

Code:
Dim doesFileExist As Boolean
doesFileExist = checkFile("http://www.mywebserver.com/files/" & Filename)
MsgBox "Remote file exists - " & doesFileExist

which returns a true or false. But for the life of me I can't return the filesize of the file on the remote server....

If you can point me in the right direction, I'd be extremely happy :)

Thanks
 
doh... my mind is fuddled! It was such a long time ago that I put all this code together...

The function checkfile is:
Code:
Function checkFile(ByRef sURL As String) As Boolean


Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "HEAD", sURL, False
oXHTTP.Send
    If oXHTTP.Status = 200 Then
        'FileSize = oXHTTP.getResponseHeader("Content-Length")
        'MsgBox "exists" & sURL
        checkFile = True
    Else
        'FileSize = -1
        'MsgBox "doesn't exist" & sURL
        checkFile = False
    End If


End Function

FileSize is commented out.... So would I do something like:


Code:
Dim remoteFileSize As Long
remoteFileSize = checkFile("[COLOR=#333333]http://www.mywebserver.com/files/" & Filename[/COLOR])
MsgBox remoteFileSize
 
Last edited:
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Yes, but to prevent 2 calls, you may want something like:

Rich (BB code):
Function checkFile(ByVal sURL As String, Optional ByRef fileSize as Long) As Boolean


    Static oXHTTP As Object
    
    if oXHTTP is Nothing then Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
    
    fileSize = 0
    
    oXHTTP.Open "HEAD", sURL, False
    oXHTTP.Send
    If oXHTTP.Status = 200 Then
        fileSize = Val(oXHTTP.getResponseHeader("Content-Length"))
        'MsgBox "exists" & sURL
        checkFile = True
    Else
        fileSize = 0
        'MsgBox "doesn't exist" & sURL
        checkFile = False
    End If


End Function

That would then be called:
Rich (BB code):
Dim remoteFileSize As Long
Dim doesFileExist As Boolean
doesFileExist = checkFile("http://www.mywebserver.com/files/" & Filename, remoteFileSize)
MsgBox remoteFileSize
MsgBox doesFileExist

This then requires a singe http request to get the file size and checks whether it exists. The web server may not always return the file size should the file exist, so you need to check the status.

If the file doesn't exist or there is no length returned the file size will be zero.

Note that the fileSize is optional, so that this will not break any existing code that passes one parameter and expects a boolean result
 
Last edited:
Upvote 0
BRILLIANT!

Thanks Kyle - works a treat!!! Happy days!!!
 
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,450
Members
449,453
Latest member
jayeshw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top