• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Search for a directory

I want to determine if a particular directory exists while in an Access form (2003).

I am using the below code that works fine but I want to only search for one level of directory not all the sub directories in the folder.  The Main path is P:\Rental Jobs and under that directory is a directory for each job number in our access table  (like P:\Rental jobs\20456.  ) But I dd not want to look for any subfolders beyond the Job Number directory. I am not sure of the different settings available in the code.  Any suggestions ?

Public Function FindMyFolder(startfolder As String, searchname As String) As String

Static fso As scripting.FileSystemObject
If fso Is Nothing Then Set fso = New scripting.FileSystemObject

Dim folder As scripting.folder
Dim result As String

Set folder = fso.GetFolder(startfolder)

Dim subfolder As scripting.folder
For Each subfolder In folder.SubFolders
  If subfolder.Name = searchname Then
    result = subfolder.path
    GoTo done
    result = FindMyFolder(subfolder.path, searchname)
    Me.txtSearch = subfolder.path
    MsgBox ("Search Directory: " & txtSearch), vbInformation
    If result <> Empty Then GoTo done
  End If

FindMyFolder = result

End Function
1 Solution
Try using the following function:
Public Function FolderExists(strFolder As String) As Boolean
On Error GoTo ProcExit
  If (GetAttr(strFolder) And vbDirectory) = vbDirectory Then
    FolderExists = True
  End If
End Function

Open in new window

Simply pass it the full path to the folder you want to check and it will return True or False.

Best wishes,
Graham Mandeno [Access MVP 1996-2013]
ChandlerBoundAuthor Commented:
Worked perfectly and quickly as  my method was way too slow!  Thank you for the quick response.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now