Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apply FileSystemObject methods to url folders & files

Status
Not open for further replies.

Argh2

Programmer
Sep 21, 2006
12
US
There are several threads about how to FileExists and FolderExists, etc. but they assume the filespec or folderspec argument is on your computer or on a mapped network drive (e.g., "M:\folder\file.xls"). I get inconsistent or unusable results when applying them to urls (e.g., "
I'm looking for equivalent functionality to the FileSystemObject methods applicable to files or folders on an intranet or the internet.

(1) Is there a FileExists that accepts url-type arguments?
(2) How about a FolderExists?
(3) If not, is there a workaround? (e.g., a way to get FileSystemObject methods to give consistently useful results with url arguments)

Thanks
-Argh2
 
1) No
2) No
3) Not that I know of.

The problem stems from the fact that FileSystemObject has direct access to the OS and, well, the File System.

When you access an URL you must go through a IP protocol to get whatever it is. There is no direct, OS level, access to the File System.

There is a new version of WinHTTP, but as far as I can see there is no way to get the equivalent FileExists, FolderExists.

You get an error when you try to access a file that does not exist, or a folder that does not exist - but this is hardly the same as querying about its existence.

Gerry
My paintings and sculpture
 
Argh2,
fumei has peaked my interest. You could try using WinHTTP and see what type of errors it returns. Here is a sample (for the old version?) if you want to test it out.
Code:
Function URLIsValid(URL As String) As Boolean
'Requires Microsoft WinHTTP Services (winhttp.dll)
'to be registered on the host system.
'Version 5.1 was used for this sample in Excel 2000 SR-1
On Error GoTo URLIsValid_Error
Dim objHTTPRequest As Object
Set objHTTPRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
With objHTTPRequest
  .Open "GET", URL
  .Send
End With
URLIsValid = True

Clean_Up:
Set objHTTPRequest = Nothing
Exit Function

URLIsValid_Error:
Debug.Print Err.Number, Err.Description
URLIsValid = False
Resume Clean_Up
End Function

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 

fumei & CautionMP:
Thanks for your replies -- especially the code, which got me testing it right away!

A little testing showed that the error trapping approach is overly accepting...as long as the servername (front of url) is ok, no error is raised, even if the folder and/or file names are fictional.

But msdn shows that WinHTTP has a .status property which = 404 when the url is not found...so the revised code is:

Code:
Function URLIsValid(URL As String) As Boolean
'Requires Microsoft WinHTTP Services (winhttp.dll)
'to be registered on the host system.
'Version 5.1 was used for this sample in Excel 2000 SR-1
On Error GoTo URLIsValid_Error
Dim objHTTPRequest As Object
Set objHTTPRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
With objHTTPRequest
  .Open "GET", URL
  .Send
  [COLOR=blue]URLIsValid = .Status <> 404  'This is the test[/color]
End With

Clean_Up:
Set objHTTPRequest = Nothing
Exit Function

URLIsValid_Error:  '[COLOR=blue]It hasn't hit this error 
'                   with bad files or folders[/color]
Debug.Print Err.Number, Err.Description
URLIsValid = False
Resume Clean_Up
End Function


This duplicates fileexists and folderexists.
I'm using it for files on an intranet, behind a firewall and it works fast & reliably so far. It times out when used for files on the internet, but perhaps that's the firewall.

Thanks!
-Argh2
 
Tried updated code from another computer, behind a personal firewall, with internet urls -- it works fine and responds immediately:

FileExists equivalent:
?urlisvalid("True
?urlisvalid("False

FolderExists equivalent:
?urlisvalid("True
?urlisvalid("False

-Argh2
 
I am not sure I would agree it gives equivalency, but as a workaround it gives useable functionality.

It sometimes seems WinHTTP is kind of a forgotten library within the VBA world . It has some very interesting features. I use it a fair amount in Word documents, pulling stuff from within our intranet. For example, our business graphics are kept at one location. Documents pull them fresh using WinHTTP. This is a handy thing when we have 33,000 users, 2,700 locations spread out coast to coast to coast (Atlantic, Pacific and Arctic). Updates are easy. The documents always get whatever IS the latest.

This is a moot point (perhaps.....) if you are running SharePoint Portals etc. etc. - which we are not.

Inside the firewall it is quite fast enough. Requests out through the firewall are a bit slower, so I was interested in hearing that you had good response with it.

Gerry
My paintings and sculpture
 
A little testing showed that the error trapping approach is overly accepting...as long as the servername (front of url) is ok, no error is raised, even if the folder and/or file names are fictional.
You may want to explore the use of WinHTTPCrackUrl. It is a way of parsing a URL for components.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top