[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

API to find files

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
escheider
Asked:
escheider
  • 2
1 Solution
 
hesCommented:
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
 
escheiderAuthor Commented:
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
 
escheiderAuthor Commented:
I Figured it out...this works great..thanks for the quick responses.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now