Check if file exist on http Server

Freshman

Registered User.
Local time
Tomorrow, 00:24
Joined
May 21, 2010
Messages
437
Hi all,

I found and modified this code to allow me to check (fast) if a file exist on a http server or not (without downloading it).

Code:
Public Function CheckNetFile(WebFile As String)
Dim XmlHttpReq As Object
Set XmlHttpReq = CreateObject("Microsoft.XMLHTTP")
XmlHttpReq.Open "HEAD", WebFile, False
XmlHttpReq.Send
If XmlHttpReq.Status = 200 Then CheckNet = 1 Else CheckNet = 0
Set XmlHttpReq = Nothing
End Function
 
Public Function Test()
CheckNetFile ("[URL]http://www.mysite.co.za/test/test.txt[/URL]")
MsgBox CheckNet
End Function

In the above code I managed to declare Webfile as a string so I can call on it later but one thing I still need to tweak is to also get a result out of the test code in a better way than having to publically declare the "CheckNet" variable somewhere else in my app.

First prize would something like this:

If CheckNetFile ("http://www.mysite.co.za/test/test.txt") = 1 then "Yes" else "No"

How do I build the 'booleon check' into the original function?

Thanks
Pierre
 
Seeing as it is already a function, ..... change....
Public Function CheckNetFile(WebFile As String) as boolean

If XmlHttpReq.Status = 200 Then CheckNetFile= 1 Else CheckNetFile= 0

Then you can do
Code:
Public sub Test()
Msgbox CheckNetFile ("http://www.mysite.co.za/test/test.txt")
End sub

Keep in mind, functions can and should return values to whereever they were called from.
While subs just execute code and return nothing.
 
Hi Namlian,

Thanks for the responds but I might be missing something.
I added the "as boolean" at the end of the Public Function but the result from the Msgbox result in 'false' even if the file do exist.
Can you test it on your side please?

I've added a real file here to make it easier for you to test:
http://www.hris.co.za/ls/downloads/test.txt

Thanks
Pierre
 
Try..
Code:
Public Function CheckNetFile(WebFile As String) As Boolean
    Dim XmlHttpReq As Object
    Set XmlHttpReq = CreateObject("Microsoft.XMLHTTP")
    XmlHttpReq.Open "HEAD", WebFile, False
    XmlHttpReq.Send
    If XmlHttpReq.Status = 200 Then
        CheckNetFile = True
    Else
        CheckNetFile = False
    End If
    Set XmlHttpReq = Nothing
End Function
 
Public Sub Test()
    MsgBox CheckNetFile("http://www.hris.co.za/ls/downloads/test.txt")
End Sub
 
Hi Paul, you da man - thanks.
I missed a small thing in Namlian's responds.
Tumbs-up to you both.
Pierre
 

Users who are viewing this thread

Back
Top Bottom