VB icon

A Find File Procedure No Looping

Email
Submitted on: 1/25/2015 4:48:00 AM
By: carņal (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: VB 5.0, VB 6.0
Views: 900
 
     This procedure finds a file and returns the full path, provided a partial path it will search all sub dirs. No looping only 1 API - this is the kind. I can't really take credit for putting an api to use, just sharing something that you might find useful.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :A Find File Procedure No Looping
'**************************************
Declare Function SearchTreeForFile Lib "IMAGEHLP.DLL" (ByVal lpRootPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Find File Procedure No Looping
' Description:This procedure finds a file and returns the full path, provided a partial path it will search all sub dirs. No looping only 1 API - this is the kind.
I can't really take credit for putting an api to use, just sharing something that you might find useful.
' By: carņal (from psc cd)
'
' Inputs:sFile: File name to find.
sRootPath: Path to begin the search in
'
' Returns:Returns the full file path if found otherwise returns null string
'
' Assumes:I know of two limitations:
1. The api has a maximum path depth of 32 directories (this is documented).
If a directory that the current user does not have access rights to resides in the directory that the search starts in the api call will fail without warning (this is NOT documented).
'**************************************

Private Function FindFile(sFile As String, sRootPath As String) As String
 ' Search for the file specified and return the full path if found
 Dim sPathBuffer As String
 Dim iEnd As Integer
 
 'Allocate some buffer space (you may need more)
 sPathBuffer = Space(512)
 
 If SearchTreeForFile(sRootPath, sFile, sPathBuffer) Then
'Strip off the null string that will be returned following the path name
iEnd = InStr(1, sPathBuffer, vbNullChar, vbTextCompare)
sPathBuffer = Left$(sPathBuffer, iEnd - 1)
FindFile = sPathBuffer
 Else
FindFile = vbNullString
 End If
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.