Author : Dipen Anovadia
Date Submitted : 8/25/2005
Category : File Manipulation
Compatibility : VB 6,VB 5
This code has been accessed 4603 times.
Task : Easily check whether any file or folder exists or not
using 1. 'FileSystemObject' 2. 'Dir' (VB Built-In)
Declarations
Code
Public Function FolderPathExists(sDir 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
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