Solved

API to find files

Posted on 2002-07-18
4
142 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now