cybergirl
asked on
Searching for a file.
Does anybody know of a function that will search the
c: drive for a file and return the full path to it?
c: drive for a file and return the full path to it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.vb-world.net/tips/tip59.html
Public Function fFindthePath(ByVal startDir As String, ByVal myFileName As String) As String
Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim new_files As String
Dim attr As Integer
Dim theDir As String
Dim theSubDir As String
On Error Resume Next
' See If the file in this directory.
If Right(startDir, 1) = "\" Then startDir = Left(startDir, Len(startDir) - 1)
theDir = startDir & "\"
fname = Dir(theDir & myFileName, vbNormal)
If fname <> "" Then
fFindthePath = theDir
Exit Function
End If
' Not find in the main dir, Get the list of subdirectories.
fname = Dir(startDir & "\*.*", vbDirectory)
Do While fname <> ""
' Skip this dir and its parent.
attr = 0 ' In case there's an error.
attr = GetAttr(startDir & "\" & fname)
If fname <> "." And fname <> ".." And _
(attr And vbDirectory) <> 0 _
Then
num_dirs = num_dirs + 1
ReDim Preserve dir_names(1 To num_dirs)
dir_names(num_dirs) = fname
End If
fname = Dir()
Loop
' Search the other directories.
For i = 1 To num_dirs
theSubDir = fFindthePath(startDir & "\" & dir_names(i), myFileName)
If theSubDir > "" Then
fFindthePath = theSubDir
Exit Function
End If
Next i
End Function
Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim new_files As String
Dim attr As Integer
Dim theDir As String
Dim theSubDir As String
On Error Resume Next
' See If the file in this directory.
If Right(startDir, 1) = "\" Then startDir = Left(startDir, Len(startDir) - 1)
theDir = startDir & "\"
fname = Dir(theDir & myFileName, vbNormal)
If fname <> "" Then
fFindthePath = theDir
Exit Function
End If
' Not find in the main dir, Get the list of subdirectories.
fname = Dir(startDir & "\*.*", vbDirectory)
Do While fname <> ""
' Skip this dir and its parent.
attr = 0 ' In case there's an error.
attr = GetAttr(startDir & "\" & fname)
If fname <> "." And fname <> ".." And _
(attr And vbDirectory) <> 0 _
Then
num_dirs = num_dirs + 1
ReDim Preserve dir_names(1 To num_dirs)
dir_names(num_dirs) = fname
End If
fname = Dir()
Loop
' Search the other directories.
For i = 1 To num_dirs
theSubDir = fFindthePath(startDir & "\" & dir_names(i), myFileName)
If theSubDir > "" Then
fFindthePath = theSubDir
Exit Function
End If
Next i
End Function
to call, pass the parameter:
Dim lstring as String
lString = fFindthePath("C:\", theFileName)
Dim lstring as String
lString = fFindthePath("C:\", theFileName)
ASKER
Thanks...it worked great.