Home > How-To Library > File/Folders Handling

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

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'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")

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.