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?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

dabelleiConnect With a Mentor Commented:
go to

There a demo on how using API to search for file on a drive.

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 _
            num_dirs = num_dirs + 1
            ReDim Preserve dir_names(1 To num_dirs)
            dir_names(num_dirs) = fname
        End If
        fname = Dir()
    ' 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)
cybergirlAuthor Commented: worked great.
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.

All Courses

From novice to tech pro — start learning today.