Solved

API to find files

Posted on 2002-07-18
4
141 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
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
hes earned 25 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now