CodeItBetter Programming Another VB Programming Blog

How to check whether the given folder exists or not (in four ways)

Posted on July 12, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'File/Folder Handling - How to check whether the given folder exists or not (in four ways)
'Using Dir:

Public Function FolderExistsUsingDir(ByVal strFolder As String) As Boolean
    On Error Resume Next
    FolderExistsUsingDir = (Dir$(strFolder, vbDirectory) <> vbNullString)
End Function
'How can I call this function:
'Debug.Print FolderExistsUsingDir("C:\Temp\b1")

'Using FSO:

Public Function FolderExistsUsingFSO(ByVal sFolder As String) As Boolean
'Set a reference to "Microsoft Scripting Runtime"
    Dim FSO As Scripting.FileSystemObject
    Set FSO = New Scripting.FileSystemObject
    FolderExistsUsingFSO = FSO.FolderExists(sFolder)
    Set FSO = Nothing
End Function
'How can I call this function:
'Debug.Print FolderExistsUsingFSO("C:\Temp\b1")

'Using Attribute:

Public Function FolderExistsUsingAttrib(ByVal sFolder As String) As Boolean
    Dim lRetVal As Long
 
    On Error GoTo FileDoesntExist:
    lRetVal = GetAttr(sFolder)
    FolderExistsUsingAttrib = True
    Exit Function
 
FileDoesntExist:
    FolderExistsUsingAttrib = False
End Function
'How can I call this function:
'Debug.Print FolderExistsUsingAttrib("C:\Temp\b1")

'Using API:

Private Declare Function PathIsDirectory Lib "shlwapi.dll" Alias "PathIsDirectoryA" _
    (ByVal pszPath As String) As Long
 
Public Function FolderExistsUsingAPI(ByVal sFolder As String) As Boolean
    FolderExistsUsingAPI = PathIsDirectory(sFolder)
End Function
'How can I call this function:
'Debug.Print FolderExistsUsingAPI("C:\Temp\b1")
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.