I am given a bridge plan number which is a 4 digit number. I need to find a folder corresponding that bridge. The folder name will carry the bridge plan number and some other random text (eg "1234- washington street"). I've written some code that is able to achieve this but it is very slow. I was wondering if someone can com up with a more efficient way of doing it. Thanks.
Public FSO As New FileSystemObject
Public Function FoundPlan(bridge_plan As String)
Dim objFolder As Folder
Dim planFolder As Folder
Dim Path As String
Dim i As String
Path = "G:\some\path"
'This is the directory path that carries my list of folders
Set objFolder = FSO.GetFolder(Path)
If Not Len(bridge_plan) = 4 Then
FoundPlan = ""
Exit Function
End If
'If the given plan number is anything except 4 digits, the function returns
'nothing and exits
For Each planFolder In objFolder.SubFolders
If Not InStr(planFolder, bridge_plan) = 0 Then
FoundPlan = Path & planFolder
Exit For
End If
Next planFolder
'For each subfolder in my directory I use instr to search for my number
'inside the folder path.
End Function
Try this instead. It doesn't need to loop through each folder
Public Function FoundPlan(bridge_plan As String) As String
Dim planFolder As String, Path As String
Path = "G:\some\path\"
'This is the directory path that carries my list of folders
If Not Len(bridge_plan) = 4 Then Exit Function
planFolder = Dir(Path & bridge_plan & "*", vbDirectory)
If Not planFolder = vbNullString Then FoundPlan = Path & planFolder
End Function
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments