Unanswered: List Subdirectory and Filenames Using Dir Function
Can anyone tell me how to retrieve the filenames, and subdirectory names using the dir function or any other method ?
What I am trying to do is have the system read through every filename and subdirectory folder so I can enter them into a table to use later in a search routine in my program. I have thousands of word docs and pdf's that have logical names, and would like to search them all from one location and this will help.
I have experimented with the dir function extensively and have searched this forum, & MVPS and even resorted to searching Microsoft.
If all you want to do is search for a specific file then you can simply use a API function to do it and its relatively fast. Putting the paths and file name into a Table is faster but this is only good if the file is never moved otherwise the table would need to be updated as it would be also should a new file be supplied to the system.
It's much easier (I think) to place all the files in question on a single drive then search thaat drive for the actual location. I also like to use Windows API functions since additional references are not required.
Place the Follwing code into a Database main code module:
' Used for locating a file:
Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Public Function FindFile(ByVal SrchStart As String, ByVal SrchString _
As String) As String
' Change the Mouse Pointer to the Busy Hourglass.
Screen.MousePointer = 11
Dim Ret As Long
Const MAX_PATH = 260
' create a buffer string.
FindFile = String(MAX_PATH, 0)
' returns 1 when successfull, 0 when failed.
Ret = SearchTreeForFile(SrchStart, SrchString, FindFile)
If Ret <> 0 Then
' File Found..return the Path.
FindFile = left$(FindFile, InStr(1, FindFile, Chr$(0)) - 1)
' File isn't found...return a empty string.
FindFile = ""
' Change the Mouse Pointer back to normal.
Screen.MousePointer = 0
Now...to use this function you could do something like this:
MsgBox FindFile("D:\", "myPDFFileName.pdf")
If you want to use wildcards in your search then check out the FindFileFirst and FindFileNext API functions.
Self Taught In ALL Environments.....And It Shows!