Detecting if a path exists... 
Author Message
 Detecting if a path exists...

Is there a way to test a path to see if it exists in VB? I have a dir()
call that crashes if the drive or path does not exist and I would like
to not use standard error handling to solve this problem. Thanks.

Bejay Cole



Sun, 23 Jul 2000 03:00:00 GMT  
 Detecting if a path exists...

Seems like a stupid answer but why not just use dir$ to look for the path
and if its not there then "" will be returned.

Cheers
MickyBoy

Quote:

>Is there a way to test a path to see if it exists in VB? I have a dir()
>call that crashes if the drive or path does not exist and I would like
>to not use standard error handling to solve this problem. Thanks.

>Bejay Cole



Sun, 23 Jul 2000 03:00:00 GMT  
 Detecting if a path exists...

Here's one way using the API.  It returns True if the path exists, and false if not. It
doesn't matter if the path passes is in long or short filename format.

Declare Function GetShortPathName Lib "kernel32" _
   Alias "GetShortPathNameA" _
   (ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, _
   ByVal cchBuffer As Long) As Long

Function PathExists(pathToCheck As String) As String

    Dim dummy As String
    Dim nSize As Long

    dummy = Space$(255)
    nSize = Len(dummy)
    PathExists = GetShortPathName(pathToCheck, dummy, nSize) <> 0

 End Function

Private Sub Command1_Click()

 Print PathExists("c:\win")

End Sub

--

Randy Birch, MVP Visual Basic

VBnet, The Visual Basic Developers Resource Centre
http://home.sprynet.com/sprynet/rasanen/vbnet/default.htm

:Is there a way to test a path to see if it exists in VB? I have a dir()
:call that crashes if the drive or path does not exist and I would like
:to not use standard error handling to solve this problem. Thanks.
:
:Bejay Cole
:



Sun, 23 Jul 2000 03:00:00 GMT  
 Detecting if a path exists...

Quote:

>Is there a way to test a path to see if it exists in VB? I have a dir()
>call that crashes if the drive or path does not exist and I would like
>to not use standard error handling to solve this problem. Thanks.

Why not?

    Function exists (Byval path$) as integer 'or boolean
        Dim file$
        On error Resume Next
        file$ = Dir$
        On Error goto 0
        exists = (file$>"")
    End Function

You may resort to API functions, if you like... Openfile() (with
OF_EXIST) seems like a good candidate.

HTH,
Bart.



Mon, 24 Jul 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. fRefreshLinks Doesn't work if path doesn't exist

2. Existing Database Path

3. Checking if folder/path exists: Outlook Form

4. Check if path exists

5. How to check if a path exists

6. File exists in Dos Path?

7. Finding if File and Path Exists?

8. Reading Target path from *existing* shortcut files.

9. Path exists or not?

10. Path exists or not?

11. How do you tell if PATH exists

12. Check if UNC path exists

 

 
Powered by phpBB® Forum Software