Results 1 to 4 of 4

Thread: What is wrong with this code??

  1. #1

    Thread Starter
    Junior Member mharley's Avatar
    Join Date
    Jan 2002
    Location
    Nova Scotia, Canada
    Posts
    24

    What is wrong with this code??

    I am attempting to write a simple search routine, but for some reason the FindFirstFile api call always returns a -1 (no files found, or error), and I can't figure out why. Can someone please help me spot my mistake? Thanks.

    Code:
    Dim hsearch As Long
    Dim findinfo As WIN32_FIND_DATA
    Dim success As Long
    Dim buffer As Long
    Dim retval As Long
    
    FileAndPath = Path & FileName
    hsearch = FindFirstFile(FileAndPath, findinfo)
    If hsearch = -1 Then
      frmMain.Caption = "No Files Found"
      Exit Sub
    End If
    
    Do
      buffer = Left(findinfo.cFileName, InStr(findinfo.cFileName,   
          vbNullChar) - 1)
       cboFileList.AddItem buffer
       success = FindNextFile(hsearch, findinfo)
    Loop Until success = 0
    
    retval = FindClose(hsearch)
    Cheers!

    Mark

  2. #2

    Thread Starter
    Junior Member mharley's Avatar
    Join Date
    Jan 2002
    Location
    Nova Scotia, Canada
    Posts
    24

    Update

    Ok, I fixed this, to an extent. I have gotten the search to work properly, but now when I try to copy the file, I get the following error message "Compiler Error: Wrong Number of Arguments or invalid property assignment", and it highlights CopyFile. I have looked at my CopyFile API over and over, and I don't understand what the issue is. I will post the code below:

    Code:
    ....
    Dim TargetFolderAndFile As String
    Dim retval As Long
    ....
    retval = CopyFile(FileAndPath, TargetFolderAndFile, 1)
    Cheers!

    Mark

  3. #3

    Thread Starter
    Junior Member mharley's Avatar
    Join Date
    Jan 2002
    Location
    Nova Scotia, Canada
    Posts
    24

    Exclamation Another Update

    Also, is there a way to get a files full path when using the FindFirstFile and FindNextFile api???
    Cheers!

    Mark

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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, SearchStr As String, FileCount As Integer, DirCount As Integer)
    42.     'KPD-Team 1999
    43.     'E-Mail: [email][email protected][/email]
    44.     'URL: [url]http://www.allapi.net/[/url]
    45.  
    46.     Dim FileName As String ' Walking filename variable...
    47.     Dim DirName As String ' SubDirectory Name
    48.     Dim dirNames() As String ' Buffer for directory name entries
    49.     Dim nDir As Integer ' Number of directories in this path
    50.     Dim i As Integer ' For-loop counter...
    51.     Dim hSearch As Long ' Search Handle
    52.     Dim WFD As WIN32_FIND_DATA
    53.     Dim Cont As Integer
    54.     If Right(path, 1) <> "\" Then path = path & "\"
    55.     ' Search for subdirectories.
    56.     nDir = 0
    57.     ReDim dirNames(nDir)
    58.     Cont = True
    59.     hSearch = FindFirstFile(path & "*", WFD)
    60.     If hSearch <> INVALID_HANDLE_VALUE Then
    61.         Do While Cont
    62.         DirName = StripNulls(WFD.cFileName)
    63.         ' Ignore the current and encompassing directories.
    64.         If (DirName <> ".") And (DirName <> "..") Then
    65.             ' Check for directory with bitwise comparison.
    66.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    67.                 dirNames(nDir) = DirName
    68.                 DirCount = DirCount + 1
    69.                 nDir = nDir + 1
    70.                 ReDim Preserve dirNames(nDir)
    71.             End If
    72.         End If
    73.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    74.         Loop
    75.         Cont = FindClose(hSearch)
    76.     End If
    77.     ' Walk through this directory and sum file sizes.
    78.     hSearch = FindFirstFile(path & SearchStr, WFD)
    79.     Cont = True
    80.     If hSearch <> INVALID_HANDLE_VALUE Then
    81.         While Cont
    82.             FileName = StripNulls(WFD.cFileName)
    83.             If (FileName <> ".") And (FileName <> "..") Then
    84.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    85.                 FileCount = FileCount + 1
    86.                 List1.AddItem path & FileName
    87.             End If
    88.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    89.         Wend
    90.         Cont = FindClose(hSearch)
    91.     End If
    92.     ' If there are sub-directories...
    93.     If nDir > 0 Then
    94.         ' Recursively walk into them...
    95.         For i = 0 To nDir - 1
    96.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
    97.         Next i
    98.     End If
    99. End Function
    100. Sub Command1_Click()
    101.     Dim SearchPath As String, FindStr As String
    102.     Dim FileSize As Long
    103.     Dim NumFiles As Integer, NumDirs As Integer
    104.     Screen.MousePointer = vbHourglass
    105.     List1.Clear
    106.     SearchPath = Text1.Text
    107.     FindStr = Text2.Text
    108.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    109.     Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
    110.     Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    111.     Screen.MousePointer = vbDefault
    112. End Sub

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