Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching for a file.

Posted on 1998-11-10
5
Medium Priority
?
154 Views
Last Modified: 2012-05-04
Does anybody know of a function that will search the
c: drive for a file and return the full path to it?
0
Comment
Question by:cybergirl
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
dabellei earned 120 total points
ID: 1444075
go to http://www.vb-word.net/tips/tip59.html

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

0
 
LVL 2

Expert Comment

by:dabellei
ID: 1444076
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1444077
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
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1444078
to call, pass the parameter:
Dim lstring as String
lString = fFindthePath("C:\", theFileName)
0
 

Author Comment

by:cybergirl
ID: 1444079
Thanks...it worked great.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question