Questions And Answers

More Tutorials

VBA Working With Files and Directories Without Using FileSystemObject

Determining If Folders and Files Exist


To determine if a file exists, simply pass the filename to the Dir$ function and test to see if it returns a result. Note that Dir$ supports wild-cards, so to test for a specific file, the passed pathName should to be tested to ensure that it does not contain them. The sample below raises an error - if this isn't the desired behavior, the function can be changed to simply return False.

Public Function FileExists(pathName As String) As Boolean
 If InStr(1, pathName, "*") Or InStr(1, pathName, "?") Then
 'Exit Function 'Return False on wild-cards.
 Err.Raise 52 'Raise error on wild-cards.
 End If
 FileExists = Dir$(pathName) <> vbNullString
End Function

Folders (Dir$ method):

The Dir$() function can also be used to determine if a folder exists by specifying passing vbDirectory for the optional attributes parameter. In this case, the passed pathName value must end with a path separator (\), as matching filenames will cause false positives. Keep in mind that wild-cards are only allowed after the last path separator, so the example function below will throw a run-time error 52 - "Bad file name or number" if the input contains a wild-card. If this isn't the desired behavior, uncomment On Error Resume Next at the top of the function. Also remember that Dir$ supports relative file paths (i.e. ..\Foo\Bar), so results are only guaranteed to be valid as long as the current working directory is not changed.

Public Function FolderExists(ByVal pathName As String) As Boolean
 'Uncomment the "On Error" line if paths with wild-cards should return False
 'instead of raising an error.
 'On Error Resume Next
 If pathName = vbNullString Or Right$(pathName, 1) <> "\" Then
 Exit Function
 End If
 FolderExists = Dir$(pathName, vbDirectory) <> vbNullString
End Function

Folders (ChDir method):

The ChDir statement can also be used to test if a folder exists. Note that this method will temporarily change the environment that VBA is running in, so if that is a consideration, the Dir$ method should be used instead. It does have the advantage of being much less forgiving with its parameter. This method also supports relative file paths, so has the same caveat as the Dir$ method.

Public Function FolderExists(ByVal pathName As String) As Boolean
 'Cache the current working directory
 Dim cached As String
 cached = CurDir$
 On Error Resume Next
ChDir pathName
 FolderExists = Err.Number = 0
 On Error GoTo 0
 'Change back to the cached working directory.
 ChDir cached
End Function


In this page (written and validated by ) you learned about VBA Working With Files and Directories Without Using FileSystemObject . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Reading 2GB files in binary in VBA and File Hashes.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.