Path exists or not? 
Author Message
 Path exists or not?

Hello,

How can you find out if a specific path exists or not?
(I don't need to find out if a file belongs to that path, only if the path
exists)
If it doens't exist I want to create it.

The Dir function causes an error 76 (Path doesn't exist!). I can catch that
error (On Error Goto ...), but isn't there a more elegant method?

Laurent Meurisse
GOODYEAR Fabric Plant Luxembourg



Mon, 04 Dec 2000 03:00:00 GMT  
 Path exists or not?

You can use the PathIsDirectory API out of shlwapi.dll to check for a valid
directory.



Quote:
> Hello,

> How can you find out if a specific path exists or not?
> (I don't need to find out if a file belongs to that path, only if the
path
> exists)
> If it doens't exist I want to create it.

> The Dir function causes an error 76 (Path doesn't exist!). I can catch
that
> error (On Error Goto ...), but isn't there a more elegant method?



Mon, 04 Dec 2000 03:00:00 GMT  
 Path exists or not?

Quote:

> Hello,

> How can you find out if a specific path exists or not?
> (I don't need to find out if a file belongs to that path, only if the path
> exists)
> If it doens't exist I want to create it.

> The Dir function causes an error 76 (Path doesn't exist!). I can catch that
> error (On Error Goto ...), but isn't there a more elegant method?

Hi,

Try

  pathname="something"
  print dir("d:\"& pathname & "\*.*",16)<>""

Michaela



Mon, 04 Dec 2000 03:00:00 GMT  
 Path exists or not?

Laurent
Here are three functions I use for directory creating etc.
you are welcome to them as is.

Roy Low

www.low.net.au

Sub DirCreate(ByVal NewPath$, errorcode%)
'   Sub to create a directory NewPath$ if it does not exist already
'NewPath$ = "d:\a\b\c"
errorcode% = 0
If direxist%(NewPath$) Then Exit Sub
'Make sure it is a full path without a terminating \
NewPath$ = Trim$(NewPath$)
If InStr(NewPath$, " ") <> 0 Then
    errorcode% = 3
    Exit Sub
End If
If Mid$(NewPath$, 2, 2) <> ":\" Then
    errorcode% = 1
    Exit Sub
End If
NewPath$ = convpathStr$(NewPath$, "")
n% = CountStr%(NewPath$, "\", last%)
On Error GoTo BadDirStr
For i% = 2 To n%
    s$ = getfldstr$(NewPath$, "\", i%, X%)
    d$ = Left$(NewPath$, X% - 2)
    If Not direxist%(d$) Then
        MkDir d$
    End If
Next i%
MkDir NewPath$
Exit Sub
BadDirStr:
errocode% = 2
End Sub

Function direxist%(dirn$)
'       This function returns true if a directory dirn$ exists
'       otherwise false

'first remove any trailing "\"
dirn2$ = convpathStr$(dirn$, "")
If Len(dirn2$) = 0 Then Exit Function

On Local Error GoTo Dirxerr
nof% = FreeFile
Open dirn2$ & "\dum~~.t~~" For Output As nof%
direxist% = True
Close nof%
Kill dirn2$ & "\dum~~.t~~"
Exit Function
Dirxerr:
'Reset   Removed and replaced with close nof% 29/11/96
Close nof%
Select Case Err
Case 76 'path not found
    Resume dirnoex
Case 64, 68 'Invalid dir name, Device Unavailable
    Resume dirnoex      'treat as not found
Case 70 'permission denied (write protected FD when dir exists)
    s$ = Space$(20) & "WARNING" & lf$
    s$ = s$ & "The floppy disk " & Left$(dirn2$, 2) & " is Write-protected."
    s$ = s$ & lf$ & "Correct the situation and press Enter"
    MsgBox s$, 0, "Checking Existence of Directory " & dirn2$
    Resume

Case 71 'disk not ready
    s$ = "The floppy disk " & Left$(dirn2$, 2) & " is not ready."
    s$ = s$ & lf$ & "Correct the situation and press Enter"
    s$ = s$ & lf$ & "or press ESC to Quit."
    If MsgBox(s$, 1, "Checking Existence of Directory " & dirn2$) = 1 Then
        Resume
    Else
        Resume dirnoex
    End If
Case Else
    s$ = "Unexpected Error Condition."
    s$ = s$ & lf$ & "Error No.  " & Str$(Err)
    s$ = s$ & lf$ & Error$
    MsgBox s$, 0, "Checking Existence of Directory " & dirn2$
    Resume dirnoex
End Select
Resume dirnoex
dirnoex:
direxist% = False

End Function

Function dirvalid%(dirn$)
'       This function returns true if
'   a directory name dirn$ can validly be created or exists
'       otherwise false

'first remove any trailing "\"
dirn2$ = convpathStr$(dirn$, "\")
If Len(dirn2$) = 0 Then Exit Function

'   Guard against problems with unmounted or write protected Floppies

Fd$ = UCase$(Left$(dirn2$, 2))
If Fd$ = "A:" Or Fd$ = "B:" Then
    X% = direxist%(Fd$ & "\")
    Reset
End If

If direxist%(dirn$) Then
    dirvalid% = True
    Exit Function
End If
'   Dirn$ does not exist - try to make it
On Local Error GoTo Dirverr
Call DirCreate(dirn2$, errorcode%)
If errorcode% = 0 Then
    '   remove it
    RmDir dirn2$

    dirvalid% = True
    Exit Function
Else
    dirvalid% = False
    Exit Function
End If
            'MkDir dirn2$
            '   remove it
            'RmDir dirn2$

            'dirvalid% = True
            'Exit Function
Dirverr:
MsgBox "Unexpected error in DIRVALID Function." & lf$ & Error$ & Str$(Err)

Resume dirnbad
dirnbad:
dirvalid% = False

End Function

Quote:

>Hello,

>How can you find out if a specific path exists or not?



Tue, 05 Dec 2000 03:00:00 GMT  
 Path exists or not?

Quote:

>How can you find out if a specific path exists or not?
>(I don't need to find out if a file belongs to that path, only if the path
>exists)
>If it doens't exist I want to create it.

Try this:

If Dir$("d:\zac\", vbDirectory) = "" Then
  MsgBox "directory does not exist"
Else
  MsgBox "directory does exist"
End If

Change "d:\zac\" to a directory on your computer which exists and run
it and then set it to one that doesn't and run it.

Good Luck!

Take Care, Zack Jones



Tue, 05 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Path exists or not?

2. files exists or not - says that it doesn't exist

3. File existing or not existing?

4. Long Path Names give Runtime error 76 - Path not found

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

6. Existing Database Path

7. Checking if folder/path exists: Outlook Form

8. Check if path exists

9. How to check if a path exists

10. File exists in Dos Path?

11. Finding if File and Path Exists?

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

 

 
Powered by phpBB® Forum Software