
How to check if a path exists
Quote:
> >Can somebody tell me how to program a visual basic application that checks
> >if a drive, that is located on a network, exists.
> If Dir( MyPath$, vbDirectory Or vb Hidden or vbSystem ) = "" Then
> MsgBox MyPath$ + " Not found"
> End If
> or look at the FindFirst API
I believe at one time using an invalid drive letter in Dir()
generated an error ("Device not found" or somesuch). Also,
under some networks, the only way I found to determine if
a path was available was to actually create a temporary
file there. Here's the code I use:
SUB CheckPath (txt$, retcd)
'
' This routine supplies a trailing \ to a non-null path not ending : or \
'
IF LEN(txt$) > 0 THEN
IF INSTR(":\", RIGHT$(txt$, 1)) = 0 THEN
txt$ = txt$ + "\"
END IF
END IF
retcd = TRUE
CheckPathFile$ = txt$ + "CHKPATH.TMP"
IF LEN(XDir$(CheckPathFile$)) = 0 THEN
' we try to create it
ELSE
' a file exists by this name, so we were able to access the path ...
EXIT SUB
END IF
CHKNUM = FREEFILE
ON ERROR GOTO CheckPathErr1
CheckPathTry1:
openerr = FALSE
OPEN CheckPathFile$ FOR OUTPUT AS #CHKNUM
CheckPathRes1:
IF openerr THEN
retcd = FALSE
GOTO CheckPathWrapup
END IF
ON ERROR GOTO CheckPathErr3
CheckPathTry3:
closeerr = FALSE
CLOSE CHKNUM
KILL CheckPathFile$
CheckPathRes3:
IF closeerr THEN
' nothing really to do here ...
END IF
CheckPathWrapup:
ON ERROR GOTO 0
EXIT SUB
CheckPathErr1:
ecode = ERR
openerr = TRUE
RESUME CheckPathRes1
CheckPathErr3:
ecode = ERR
closeerr = TRUE
RESUME CheckPathRes3
END SUB
FUNCTION XDir$ (p$)
XDirErrFlag = FALSE
ON ERROR GOTO XDirError
IF LEN(p$) = 0 THEN ' NOTE! DIR$("") is the same
txt$ = DIR$ ' as DIR$("*.*") and is
ELSE ' NOT the same as
txt$ = DIR$(p$) ' DIR$ with no argument!
END IF
XDirResume:
ON ERROR GOTO 0
IF XDirErrFlag THEN
txt$ = ""
END IF
XDir$ = txt$
EXIT FUNCTION
XDirError:
XDirErrFlag = TRUE
RESUME XDirResume
END FUNCTION