Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Files - help with code

Status
Not open for further replies.
Aug 2, 2000
325
US
would anyone mind reviewing this code to see if anything jumps out at them as to why I'm getting a "object required" error message.
The error is occuring in the function 'FindFilesAPI'. It seems to work as long as there are no files found. Once a file is found then error.
I'm using VB6 and got this code from the good folks at All API.
Thanks in advance!
Dave


Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
'KPD-Team 1999
'E-Mail: KPDTeam@Allapi.net
'URL:
Dim FileName As String ' Walking filename variable...
Dim DirName As String ' SubDirectory Name
Dim dirNames() As String ' Buffer for directory name entries
Dim nDir As Integer ' Number of directories in this path
Dim i As Integer ' For-loop counter...
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(path, 1) <> &quot;\&quot; Then path = path & &quot;\&quot;
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & &quot;*&quot;, WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
' Ignore the current and encompassing directories.
If (DirName <> &quot;.&quot;) And (DirName <> &quot;..&quot;) Then
' Check for directory with bitwise comparison.
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
Loop
Cont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> &quot;.&quot;) And (FileName <> &quot;..&quot;) Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
FileCount = FileCount + 1
List1.AddItem path & FileName
End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If
' If there are sub-directories...
If nDir > 0 Then
' Recursively walk into them...
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & &quot;\&quot;, SearchStr, FileCount, DirCount)
Next i
End If
End Function

**********AND ON THE FORM********

Sub Command1_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer
Screen.MousePointer = vbHourglass
List1.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
Text3.Text = NumFiles & &quot; Files found in &quot; & NumDirs + 1 & &quot; Directories&quot;
Text4.Text = &quot;Size of files found under &quot; & SearchPath & &quot; = &quot; & Format(FileSize, &quot;#,###,###,##0&quot;) & &quot; Bytes&quot;
Screen.MousePointer = vbDefault
End Sub
 
The following line does not look right
Code:
FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & &quot;\&quot;, SearchStr, FileCount, DirCount)
Several issues - What is the FineFilesAPI function returning? I would also suggest that you consider using & for concatenation rather than +, especially when dealing with untyped variables, in this case, the type of FindFilesAPI.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
What about it doesn't look right? I'm just learning to work with the API so the whole thing looks off to me.

Thanks!
 
This really doesn't apply to the API (yet). You have VB function called FindFilesAPI, but you do not specify what the return type of that function is. That's a basic VB function issue, and not one where I would rely on any default processing. You are then perform either an addition or a concatenation by using a + operator. Specify the return type of the VB function, and make sure that your using the right operator with the returned values. Once the VB issues have been cleaned up, or at least clearly specified, then you can delve into the API issues.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Slowly, yet firmly the palm of my hand smacks against my forehead with a THWACK!!

I have the api functions etc... in a sep. module yet all the functions are Private...duh. So I put the code where it belongs, in the code behind the form and POOF!
It works.

Now to append the list to a table......

Thank you very much for pointing that out to me CC. I was trying every Type and nothing was working...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top