by Dipen Anovadia (19 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 25th August 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Easily check whether any file or folder exists or not using 1. 'FileSystemObject' 2. 'Dir' (VB Built-In)
'Error handling
On Error GoTo errFPE
'File System Object
Dim fso As Object
'Create object now
Set fso = CreateObject("Scripting.FileSystemObject")
'Check if exists or not
FolderPathExist = fso.FolderExists(sDir)
Exit Function
errFPE:
MsgBox Err.Description, vbCritical, "Error " & Err
Err.Clear
FolderPathExist = False
End Function
Public Function FilePathExists(sFile As String) As Boolean
'Error handling
On Error GoTo errFPE
'File System Object
Dim fso As Object
'Create object now
Set fso = CreateObject("Scripting.FileSystemObject")
'Check if exists or not
FilePathExist = fso.FileExists(sDir)
Exit Function
errFPE:
MsgBox Err.Description, vbCritical, "Error " & Err
Err.Clear
FilePathExist = False
End Function
'----COMBINE ALTERNATE----
Public Function PathExist(sPath As String, Optional bFolder As Boolean = False) As Boolean
'Error Handling
On Error GoTo errPE
Dim iAttrib As Integer
PathExist = False
sPath = Trim$(sPath)
If sPath = "" Then Exit Function
iAttrib = IIf(bFolder, vbDirectory, vbNormal)
If Len(Dir$(sPath, iAttrib)) > 0 Then PathExist = True
Exit Function
errPE:
MsgBox Err.Description, vbCritical, "Error " & Err
Err.Clear
PathExist = False
End Function
'Please tell me if there is any bug, b'coz then only one can learn more
No comments have been posted about Easily check whether any file or folder exists or not using 1. 'FileSystemObject' 2. 'Dir' (VB Buil. Why not be the first to post a comment about Easily check whether any file or folder exists or not using 1. 'FileSystemObject' 2. 'Dir' (VB Buil.