?
Solved

API to find files

Posted on 2002-07-18
4
Medium Priority
?
148 Views
Last Modified: 2010-05-02
Hello experts:

I'm have just finished the design process of an application that needs to be written.  The end result will be to search for a filename and return the full path name of the location of the file.  In addition, a return code will be passed indicating a sucess or failure of the search.

I haven't used APIs very much, and I understand there is an API that will perform this function.  Does anyone have any examples of this including source code or a link to source code?

This application will run in two environments: Win9x and Win2k

Thanks

Escheider
0
Comment
Question by:escheider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
hes earned 100 total points
ID: 7162376
Use this
Declare

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Const MAX_PATH = 260
Private Const DRIVE_CDROM = 5
Private Const DRIVE_FIXED = 3

Call this

Private Function Find_File(FileName As String) As String
  Dim tempStr As String
  Dim Ret As Long
  Dim AllDrives As String
  Dim DriveArray
  Dim lErrCode As Long
  Dim Counter As Long
  Dim szValueName As String
  Dim sUNCName As String
 
    AllDrives = String(255, Chr$(0))
    Ret = GetLogicalDriveStrings(255, AllDrives)
    AllDrives = Replace(AllDrives, Chr$(0), "")
    AllDrives = Replace(AllDrives, "\", "\,")
    DriveArray = Split(AllDrives, ",")
 
  For Counter = 0 To UBound(DriveArray) - 1
  Ret = GetDriveType(DriveArray(Counter))
 
   If GetDriveType(DriveArray(Counter)) = DRIVE_FIXED Then
   
   
   
   
    'create a buffer string
    tempStr = String(MAX_PATH, 0)
   
    'returns 1 when successfull, 0 when failed
    Ret = SearchTreeForFile(DriveArray(Counter), FileName, tempStr)
   
    If Ret <> 0 Then
        Find_File = Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
        Exit Function
    End If
   
   End If
   
  Next Counter
 
  Find_File = "NOT FOUND"
End Function
0
 
LVL 5

Expert Comment

by:rpai
ID: 7162377
0
 
LVL 4

Author Comment

by:escheider
ID: 7162409
hes:

Forgot to add one thing.  In addition to passing the filename, I will be passing the root path to search in.  For example:  I do not want to search all of C: drive, but would like to have the option to search c:\searchdir.  I will know this location and would like to pass it to this function as well, so:

Private Function Find_File(FileName As String) As String

Could be

Private Function Find_File(SearchPath as string, FileName As String) As String
0
 
LVL 4

Author Comment

by:escheider
ID: 7162456
I Figured it out...this works great..thanks for the quick responses.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month13 days, left to enroll

777 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