Solved

API to find files

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

822 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