VBScript: How to check if a file exists on a remote server

As a web site developer, I sometimes need to check if a specific file exists on a remote server. That is easily done with the ServerXMLHTTP object. As explained by Microsoft MSDN, it retrieves XML responses from a server. If you use the object to read the head of a specific url and then check its status, you will be able to determine the file existence.
Let's try to write the code!
First of all we create the object:
<% Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
Then we open it and send the request:
xmlhttp.open "HEAD", "http://www.someremotedomain.com/somefile.xxx", False
xmlhttp.send
As you can see, when opening, we use "HEAD" as request method. This is valid for IE7+ and it is used because we do not really need to get all the content (the "GET" request method will retrieve all of it). We just want to get the head. Please note that you need to change the site url.
Now what we really are looking for is the request status. Checking the status number, we determine if the file exists:
Select Case Cint(xmlhttp.status)
   Case 200, 202, 302
     Set xmlhttp = Nothing
     response.write ("File exists")
   Case Else
     Set xmlhttp = Nothing
     response.write ("Not found")
End Select
%>
We are checking for status
  • 200 - OK (standard successful http request), 
  • 202 - Accepted (request accepted for processing, but not completed), 
  • 302 - Found (via redirection).
All those statuses are a good response for us. The file is there.
Here is the complete code:
<%
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "HEAD", "http://www.someremotedomain.com/somefile.xxx", False
xmlhttp.send
Select Case Cint(xmlhttp.status)
   Case 200, 202, 302
     Set xmlhttp = Nothing
     response.write ("File exists")
   Case Else
     Set xmlhttp = Nothing
     response.write ("Not found")
End Select
%>