Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,616
- Office Version
- 365
- 2016
- Platform
- Windows
I am using this function to check whether a file exists in a particular folder.
Assume FolderPath = "M:\Scenic Posters\Scenery\Mountains\" and partialName = "Deluge".
In my testing, that folder does not contain a file "Deluge", however, does include "Deluge Restoration" This code flags the file "Deluge" as existing, when it really doesn't. How can I improve my code to exlude files names in which a single word may exist in a string. eg Water Deluge, Deluge Restoration. I have a feeling the searches for basic single words could be found found in file names such a single word within a greater string.
VBA Code:
Function FileExistsWithPartialName(FolderPath As String, partialName As String) As Boolean
Dim fileName As String
Dim exists As Boolean
exists = False
' Ensure folder path ends with a backslash
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
' Use Dir to search for files matching the partial name
fileName = Dir(FolderPath & "*" & partialName & "*")
' If a match is found, Dir returns the file name
If fileName <> "" Then
exists = True
End If
' Return the result
FileExistsWithPartialName = exists
End Function
Assume FolderPath = "M:\Scenic Posters\Scenery\Mountains\" and partialName = "Deluge".
In my testing, that folder does not contain a file "Deluge", however, does include "Deluge Restoration" This code flags the file "Deluge" as existing, when it really doesn't. How can I improve my code to exlude files names in which a single word may exist in a string. eg Water Deluge, Deluge Restoration. I have a feeling the searches for basic single words could be found found in file names such a single word within a greater string.