|
-
Jun 9th, 2008, 11:30 PM
#1
Thread Starter
Lively Member
To search a folder in a path
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?
-
Jun 10th, 2008, 03:09 AM
#2
Hyperactive Member
Re: To search a folder in a path
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
-
Jun 10th, 2008, 03:38 AM
#3
Re: To search a folder in a path
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
-
Jun 10th, 2008, 06:35 AM
#4
Re: To search a folder in a path
No real need for APIs either
Code:
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
-
Jun 10th, 2008, 07:23 AM
#5
Re: To search a folder in a path
 Originally Posted by Hack
No real need for APIs either
Code:
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.
-
Jun 10th, 2008, 07:57 AM
#6
Re: To search a folder in a path
 Originally Posted by Ellis Dee
The advantage of the API calls is that they don't co-opt the Dir stack.
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.
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|