×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Apply FileSystemObject methods to url folders & files

Apply FileSystemObject methods to url folders & files

Apply FileSystemObject methods to url folders & files

(OP)
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., "http://abc.def.ghi.com/folder/file.xls").

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

RE: Apply FileSystemObject methods to url folders & files

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

RE: Apply FileSystemObject methods to url folders & files

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

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

RE: Apply FileSystemObject methods to url folders & files

(OP)

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
  URLIsValid = .Status <> 404  'This is the test
End With

Clean_Up:
Set objHTTPRequest = Nothing
Exit Function

URLIsValid_Error:  'It hasn't hit this error
'                   with bad files or folders

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

RE: Apply FileSystemObject methods to url folders & files

(OP)
Tried updated code from another computer, behind a personal firewall, with internet urls -- it works fine and responds immediately:

FileExists equivalent:
?urlisvalid("http://www.tek-tips.com/images/logo.gif")
True
?urlisvalid("http://www.tek-tips.com/images/logo.giffff")
False

FolderExists equivalent:
?urlisvalid("http://www.tek-tips.com/images")
True
?urlisvalid("http://www.tek-tips.com/images11111111")
False

-Argh2

RE: Apply FileSystemObject methods to url folders & files

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

RE: Apply FileSystemObject methods to url folders & files

Quote:

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close