Results 1 to 4 of 4

Thread: Finding files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Posts
    69

    Finding files

    Can anyone tell me how to find out how many files I hae on a PC wih the name, let's say, "File.mdb"?

    I used the SearchTreeForFile, but this only returns the path to the first occurance. I want to be able to list all occurances of a file.

    Any ideas?

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi there Innofin,
    using the FSO might be to slow, but anyways, this is one way of doing it

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function FoldersWhereFileExists(ByVal sFolder As String, sFileName As String) As String
    4.     Dim fso As New FileSystemObject
    5.     Dim f As Folder
    6.     Dim fldr As Folder
    7.     Dim fil As File
    8.     Dim sFoldersContainingFiles As String
    9.     'set start folder
    10.     Set fldr = fso.GetFolder(sFolder)
    11.         For Each fil In fldr.Files
    12.             If UCase(fil.Name) = UCase(sFileName) Then
    13.                 sFoldersContainingFiles = sFoldersContainingFiles & fldr.Path & vbCrLf
    14.                 Exit For
    15.             End If
    16.         Next fil
    17.        
    18.         FoldersWhereFileExists = sFoldersContainingFiles
    19.        
    20.         'in order to retrieve all the subs in the subs in the subs.... uh... _
    21.         'we have to call 'ourselfs' until there are no more subfolders left...
    22.         If fldr.SubFolders.Count > 0 Then
    23.             For Each f In fldr.SubFolders
    24.                 FoldersWhereFileExists = FoldersWhereFileExists & FoldersWhereFileExists(f.Path, sFileName)
    25.             Next f
    26.         End If
    27. End Function
    28.  
    29. Private Sub Command1_Click()
    30.     MsgBox FoldersWhereFileExists("C:\", "testigs.txt")
    31. End Sub
    -= a peet post =-

  3. #3
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i've attached an example from APIGuide that i've modified slightly for you (zip file).

    this is the code in the main form though:

    VB Code:
    1. 'Create a form with a command button (command1), a list box (list1)
    2. 'and three text boxes (text1, text2, text3).
    3. 'Type in the first textbox a startingpath like c:\
    4. 'and in the second textbox you put a pattern like *.* or *.txt
    5.  
    6. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    7. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    8. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    9. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    10.  
    11. Const MAX_PATH = 260
    12. Const MAXDWORD = &HFFFF
    13. Const INVALID_HANDLE_VALUE = -1
    14. Const FILE_ATTRIBUTE_ARCHIVE = &H20
    15. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    16. Const FILE_ATTRIBUTE_HIDDEN = &H2
    17. Const FILE_ATTRIBUTE_NORMAL = &H80
    18. Const FILE_ATTRIBUTE_READONLY = &H1
    19. Const FILE_ATTRIBUTE_SYSTEM = &H4
    20. Const FILE_ATTRIBUTE_TEMPORARY = &H100
    21.  
    22. Private Type FILETIME
    23.     dwLowDateTime As Long
    24.     dwHighDateTime As Long
    25. End Type
    26.  
    27. Private Type WIN32_FIND_DATA
    28.     dwFileAttributes As Long
    29.     ftCreationTime As FILETIME
    30.     ftLastAccessTime As FILETIME
    31.     ftLastWriteTime As FILETIME
    32.     nFileSizeHigh As Long
    33.     nFileSizeLow As Long
    34.     dwReserved0 As Long
    35.     dwReserved1 As Long
    36.     cFileName As String * MAX_PATH
    37.     cAlternate As String * 14
    38. End Type
    39. Function StripNulls(OriginalStr As String) As String
    40.     If (InStr(OriginalStr, Chr(0)) > 0) Then
    41.         OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    42.     End If
    43.     StripNulls = OriginalStr
    44. End Function
    45.  
    46. Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer)
    47.     'KPD-Team 1999
    48.     'E-Mail: [email][email protected][/email]
    49.     'URL: [url]http://www.allapi.net/[/url]
    50.  
    51.     Dim FileName As String ' Walking filename variable...
    52.     Dim DirName As String ' SubDirectory Name
    53.     Dim dirNames() As String ' Buffer for directory name entries
    54.     Dim nDir As Integer ' Number of directories in this path
    55.     Dim i As Integer ' For-loop counter...
    56.     Dim hSearch As Long ' Search Handle
    57.     Dim WFD As WIN32_FIND_DATA
    58.     Dim Cont As Integer
    59.     If Right(path, 1) <> "\" Then path = path & "\"
    60.     ' Search for subdirectories.
    61.     nDir = 0
    62.     ReDim dirNames(nDir)
    63.     Cont = True
    64.     hSearch = FindFirstFile(path & "*", WFD)
    65.     If hSearch <> INVALID_HANDLE_VALUE Then
    66.         Do While Cont
    67.         DirName = StripNulls(WFD.cFileName)
    68.         ' Ignore the current and encompassing directories.
    69.         If (DirName <> ".") And (DirName <> "..") Then
    70.             ' Check for directory with bitwise comparison.
    71.             If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    72.                 dirNames(nDir) = DirName
    73.                 DirCount = DirCount + 1
    74.                 nDir = nDir + 1
    75.                 ReDim Preserve dirNames(nDir)
    76.             End If
    77.         End If
    78.         Cont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
    79.         Loop
    80.         Cont = FindClose(hSearch)
    81.     End If
    82.     ' Walk through this directory and sum file sizes.
    83.     hSearch = FindFirstFile(path & SearchStr, WFD)
    84.     Cont = True
    85.     If hSearch <> INVALID_HANDLE_VALUE Then
    86.         While Cont
    87.             FileName = StripNulls(WFD.cFileName)
    88.             If (FileName <> ".") And (FileName <> "..") Then
    89.                 FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    90.                 FileCount = FileCount + 1
    91.                 List1.AddItem path & FileName
    92.             End If
    93.             Cont = FindNextFile(hSearch, WFD) ' Get next file
    94.         Wend
    95.         Cont = FindClose(hSearch)
    96.     End If
    97.     ' If there are sub-directories...
    98.     If nDir > 0 Then
    99.         ' Recursively walk into them...
    100.         For i = 0 To nDir - 1
    101.             FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount)
    102.         Next i
    103.     End If
    104. End Function
    105. Sub Command1_Click()
    106.     Dim SearchPath As String, FindStr As String
    107.     Dim FileSize As Long
    108.     Dim NumFiles As Integer
    109.     Screen.MousePointer = vbHourglass
    110.     List1.Clear
    111.     SearchPath = Text1.Text
    112.     FindStr = Text2.Text
    113.     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles)
    114.     Text3.Text = NumFiles & " Files found"
    115.     Screen.MousePointer = vbDefault
    116. End Sub
    Attached Files Attached Files
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Posts
    69

    resolved

    Great - Thanks alot!

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