PDA

Click to See Complete Forum and Search --> : FindFirstFile API problem!!!!!


clint_22
Nov 1st, 1999, 06:14 AM
I have a piece of code that looks like this:

FindHandle = FindFirstFile(DirPath & FileSpec, FindData)
If FindHandle <> 0 Then
If FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
If Left$(FindData.cFileName, 1) <> "." And Left$(FindData.cFileName, 2) <> ".." Then
Dirs.AddItem DirPath & Trim$(FindData.cFileName) & "\", 1
End If
Else
FileCount = FileCount + 1
ReDim Preserve Dir$(FileCount)
Dir$(FileCount) = DirPath & Trim$(FindData.cFileName)
Status.Panels(1).Text = "Collecting files: " & FileCount
End If
End If

If FindHandle <> 0 Then
Do
DoEvents
FindNextHandle = FindNextFile(FindHandle, FindData)
If FindNextHandle <> 0 Then
If FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
If Left$(FindData.cFileName, 1) <> "." And Left$(FindData.cFileName, 2) <> ".." Then
Dirs.AddItem DirPath & Trim$(FindData.cFileName) & "\", 1
End If
Else
FileCount = FileCount + 1
ReDim Preserve Dir$(FileCount)
Dir$(FileCount) = DirPath & Trim$(FindData.cFileName)
Status.Panels(1).Text = "Collecting files: " & FileCount
End If
Else
Exit Do
End If
If Cancel Then Exit Sub
Loop
End If

Call FindClose(FindHandle)

The problem is if FileSpec is anything other than "*.*" in the following line:

FindHandle = FindFirstFile(DirPath & FileSpec, FindData)

How can I narrow the search by file types using FindFirstFile???

smalig
Nov 1st, 1999, 03:41 PM
Test it:


Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long

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

Private Sub cmdSearch_Click()
Dim hFile&
Dim WFD As WIN32_FIND_DATA
Dim sPath$, sFile$

sPath = "d:\"
sFile = "*.mdb"
hFile = FindFirstFile(sPath & sFile, WFD)
'hFile = FindNextFile(hFile, WFD)
If hFile <> -1 Then
MsgBox WFD.cFileName
Else
MsgBox "File not found"
End If
End Sub


Best regards.

------------------
smalig
smalig@hotmail.com
smalig.tripod.com (http://smalig.tripod.com)

clint_22
Nov 1st, 1999, 08:22 PM
I appreciate the code offering but unfortunately this code does less than what I've already got. The code I mentioned above does a recursive search of the entire drive. The problem is that it gets the underlying directories from the first find. If you specify *.txt in the first find... it doesn't get the directories. Any suggestions on how to catch the *.txt in the next find?

smalig
Nov 1st, 1999, 08:49 PM
I find a good sample for you at http://www.planet-source-code.com/
Type 'FindFirstFile' in search string.

Best regards.

------------------
smalig
smalig@hotmail.com
smalig.tripod.com (http://smalig.tripod.com)

clint_22
Nov 2nd, 1999, 04:52 AM
When I'm debugging the program,cFileName has the value plus several lines that look like "| || || || || || || || |" trailing the data. I attempted to do a Trim$ but it still had that. I was attempting to do a Right$ on the return value to check for the extension but the "if" keeps failing because of all of the spaces after the data. Is there a way to prevent this? Like I said earlier, it has something to do with the MAX_ constant in the declaration but I can't find a way around it.