i store different paths dynamically at runtime in a variable...now i should check whether a paticular folder called "\BIN" is there in my folder or not...if the folder is not there in the path then i should throw an error...how could this be done?
Printable View
i store different paths dynamically at runtime in a variable...now i should check whether a paticular folder called "\BIN" is there in my folder or not...if the folder is not there in the path then i should throw an error...how could this be done?
Code:Dim fso As Object
Set fso = CreateObject("scripting.filesystemobject")
If fso.FolderExists("C:\attachments") = False Then
MsgBox "Booooo!"
Else
MsgBox "It's here"
End If
No need to weigh down your project with an fso dependency:Code:Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
Public Function FileExists(pstrFile As String) As Boolean
FileExists = (PathFileExists(pstrFile) = 1)
If FileExists Then FileExists = (PathIsDirectory(pstrFile) = 0)
End Function
Public Function FolderExists(pstrFolder As String) As Boolean
FolderExists = (PathFileExists(pstrFolder) = 1)
If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
End Function
No real need for APIs eitherCode:Public Function FolderExists(pstrFolder As String) As Boolean
If Dir$(pstrFolder, vbDirectory) <> vbNullString Then
'it does exist
FolderExists = True
Else
'it does not exist
FolderExists = False
End If
End Function
The advantage of the API calls is that they don't co-opt the Dir stack. I got the impression that the OP may be using a Dir loop for other purposes.Quote:
Originally Posted by Hack
Totally agree, using Dir command in Functions is just not a good idea IMO and just asking for trouble especially if you re-use functions in other projects.Quote:
Originally Posted by Ellis Dee
Heres another non-API way,
Code:Public Function bDirExists(sDir As String) As Boolean
' Tests for directory existence,
' Returns False if not found, True if found.
On Error Resume Next
bDirExists = ((GetAttr(sDir) And vbDirectory) <> 0)
End Function