Results 1 to 5 of 5

Thread: FindFirstFile API problem!!!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Birmingham, Al, United States
    Posts
    10

    Post

    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???

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Posts
    232

    Post

    Test it:

    Code:
    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
    [email protected]
    smalig.tripod.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Birmingham, Al, United States
    Posts
    10

    Post

    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?

  4. #4
    Addicted Member
    Join Date
    Oct 1999
    Posts
    232

    Post

    I find a good sample for you at http://www.planet-source-code.com/
    Type 'FindFirstFile' in search string.

    Best regards.

    ------------------
    smalig
    [email protected]
    smalig.tripod.com

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Birmingham, Al, United States
    Posts
    10

    Post

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width