Results 1 to 2 of 2

Thread: API multiple string file search: 99% completed already

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    34

    API multiple string file search: 99% completed already

    this is the code i have so far...
    it searches for all files matching a list of strings and copies them to a specified directory.
    what it needs is a module level 2D string (strallsearches())
    with all the filenames in it:
    specs*.*
    *.ini
    *win*.*
    etc.
    the problem i'm experiencing is in the section after findfirstfile. it appears to cycle through the directories infinitely instead of stopping after the last subdirectory. can anyone help me?
    VB Code:
    1. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    2. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    3. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    4. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    5.  
    6. Const MAX_PATH = 260
    7. Const MAXDWORD = &HFFFF
    8. Const INVALID_HANDLE_VALUE = -1
    9. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    10. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    11. Const FILE_ATTRIBUTE_HIDDEN = &H2
    12. Const FILE_ATTRIBUTE_NORMAL = &H80
    13. Const FILE_ATTRIBUTE_READONLY = &H1
    14. Const FILE_ATTRIBUTE_SYSTEM = &H4
    15. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    16.  
    17. Private Type FILETIME
    18.     dwLowDateTime As Long
    19.     dwHighDateTime As Long
    20. End Type
    21.  
    22. Private Type WIN32_FIND_DATA
    23.     dwFileAttributes As Long
    24.     ftCreationTime As FILETIME
    25.     ftLastAccessTime As FILETIME
    26.     ftLastWriteTime As FILETIME
    27.     nFileSizeHigh As Long
    28.     nFileSizeLow As Long
    29.     dwReserved0 As Long
    30.     dwReserved1 As Long
    31.     cFileName As String * MAX_PATH
    32.     cAlternate As String * 14
    33. End Type
    34. Function StripNulls(OriginalStr As String) As String
    35.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    36.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    37.     End If
    38.     StripNulls = OriginalStr
    39. End Function
    40.  
    41. Function FindFilesAPI(path As String)
    42.     Dim tempfile As String
    43.     Dim FileName As String ' Walking filename variable...
    44.     Dim DirName As String ' SubDirectory Name
    45.     Dim dirNames() As String ' Buffer for directory name entries
    46.     Dim nDir As Integer ' Number of directories in this path
    47.     Dim i As Integer ' For-loop counter...
    48.     Dim hSearch As Long ' Search Handle
    49.     Dim WFD As WIN32_FIND_DATA
    50.     Dim Cont As Integer
    51.     If Right(path, 1) <> "\" Then path = path & "\"
    52.     ' Search for subdirectories.
    53.     nDir = 0
    54.     ReDim dirNames(nDir)
    55.     Cont = True
    56.     hSearch = FindFirstFile(path & "*", WFD)
    57.     If hSearch <> INVALID_HANDLE_VALUE Then
    58.         Do While Cont
    59.         DirName = StripNulls(WFD.cFileName)
    60.         ' Ignore the current and encompassing directories.
    61.         If (DirName <> ".") And (DirName <> "..") Then
    62.             ' Check for directory with bitwise comparison.
    63.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    64.                 dirNames(nDir) = DirName
    65.                 nDir = nDir + 1
    66.                 ReDim Preserve dirNames(nDir)
    67.             End If
    68.         End If
    69.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    70.         Loop
    71.         Cont = FindClose(hSearch)
    72.     End If
    73.     ' Walk through this directory and sum file sizes.
    74.     'On Error GoTo HandleErrors
    75.    
    76.     Dim count2 As Integer
    77.     Cont = True
    78.    
    79.     For count2 = 0 To counts - 1
    80.         hSearch = FindFirstFile(path & strAllsearches(count2), WFD)
    81.         If hSearch <> INVALID_HANDLE_VALUE Then
    82.             While Cont
    83.                 FileName = StripNulls(WFD.cFileName)
    84.                 If (FileName <> ".") And (FileName <> "..") Then
    85.                     FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    86.                     FileCopy path & FileName, Dir2.path & "\" & FileName
    87.                 End If
    88.                 Cont = FindNextFile(hSearch, WFD) ' Get next file
    89.             Wend
    90.             Cont = FindClose(hSearch)
    91.         End If
    92.     Next count2
    93.  
    94.  
    95.     ' If there are sub-directories...
    96.     If nDir > 0 Then
    97.         ' Recursively walk into them...
    98.         For i = 0 To nDir - 1
    99.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\")
    100.         Next i
    101.     End If
    102.     Exit Function
    103. HandleErrors:
    104.     'MsgBox Err.Description & ", Please try again", vbOKOnly, "Error Number " & Err.Number
    105. End Function

  2. #2
    DaoK
    Guest

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