by Jan Nawara (3 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
This code returns a true/false if a specified drive/directory/pathname exists.
This is a small, fast routine.
Inputs
A string containing a pathname must be passed.
If checking for a directory you must also set the second optional argument to True.
Assumes
'To check if a specific drive letter exists, use strings for the PathName argument that look like this (the strings themselves should not include quotation marks):
'
'"c:"
'"c:\"
'
'Eg. DriveStat= File_Exists("c:\")
'(NOTE: The backslash is optional.)
'
'To check if a specific directory exists, use strings for the PathName argument that look like this (the strings themselves should not include quotation marks). ALSO, you must use True for the second optional argument, otherwise the function will not work on all directories.:
'
'"c:\temp\"
'"c:\windows\"
'
'Eg. DirStat = File_Exists("c:\temp", True)
'
'To check if a specific file exists, use strings for the PathName argument that look like this (the strings themselves should not include quotation marks):
'
'"c:\temp\somefile.exe"
'"c:\windows\notepad.exe"
'
'Eg. FileStat = File_Exists("c:\windows\win.ini")
Code Returns
True if the pathname and/or file exists.
Otherwise it returns false.
API Declarations
Function File_Exists(ByVal PathName As String, Optional Directory As Boolean) As Boolean
'Returns True if the passed pathname exist
'Otherwise returns False
If PathName <> "" Then
If IsMissing(Directory) Or Directory = False Then
File_Exists = (Dir$(PathName) <> "")
Else
File_Exists = (Dir$(PathName, vbDirectory) <> "")
End If
End If
End Function