Checking if a directory exists


Posted by duncan on January 14, 2002 8:03 AM

I would like to check if a directory exists (not just if it exists or is empty)
Any ideas?
Thanks
D.

Posted by DK on January 14, 2002 8:12 AM

Hi,

One of a variety of ways to do it is this:-

Function CheckFolderExists(strPath As String) As Boolean
On Error Resume Next
Err.Clear
ChDir strPath
If Err.Number = 0 Then CheckFolderExists = True
End Function


and then use it in your procedure like this:-

Sub test()
Dim strFolder As String

strFolder = InputBox("Enter the folder path")

If CheckFolderExists(strFolder) = True Then
MsgBox "That exists!"
Else
MsgBox "That folder doesn't exist"
End If
End Sub


I hope this helps,
D.

Posted by Steven J on January 14, 2002 9:29 AM

Probably a dumb question, but is there a difference between all that and a macro like
Sub CheckDirectory()
If Dir("C:\My Documents\") <> "" Then
MsgBox "Yes, path exists"
Else
MsgBox "No, path does not exist."
End If
End Sub

SJ



Posted by Ivan F Moala on January 14, 2002 8:44 PM

No not a dumb Question BUT your macro is not
usable in that the dir to check is hard coded.
DKs function will check for any dir path you specify
so that it is more usable, ie you can use this
function throughout your application. When building VBA appliations that may need these
functions it makes sence to have an all up test
rather the many seperate subs.
The function is also usable within the spreadsheet
itself eg in A1 put = CheckFolderExists("C:\mine")


Ivan Probably a dumb question, but is there a difference between all that and a macro like