Results 1 to 8 of 8

Thread: Search a directory for a file including sub direcotries

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    Search a directory for a file including sub direcotries

    How do I search a directory for a file and have it search SUB dir's also? i.e searching the Directory C:\ and being able to find my file.txt which is a few levels down.

    C:\
    C:\new folder 1\
    C:\new folder 1\new folder 2\
    C:\new folder 1\new folder 2\my file.txt
    -------------------------------------------------

    My code - doesn't work sadly....

    Dim objFSO As New FileSystemObject
    Dim objFile As File
    Dim objFolder As Folder
    Dim objsubfolder As Folders

    Set objFolder = objFSO.GetFolder(DirList.List(DirList.ListIndex))

    ' Loop through each sub directory.
    For Each objsubfolder In objFolder.SubFolders

    ' Loop through each file in the sub directory.
    For Each objFile In objFolder.Files
    Debug.Print objFile.Name
    Next objFile

    Next objsubfolder
    ----------------------------------
    Any ideas? Thanks!
    - Gabe

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sample for you

    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:\new folder 1\", "file.txt")
    31. End Sub
    -= a peet post =-

  3. #3
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    Here is one using API
    VB Code:
    1. Public Type FILETIME
    2.     dwLowDateTime As Long
    3.     dwHighDateTime As Long
    4. End Type
    5.  
    6. Public Type WIN32_FIND_DATA
    7.     dwFileAttributes As Long
    8.     ftCreationTime As FILETIME
    9.     ftLastAccessTime As FILETIME
    10.     ftLastWriteTime As FILETIME
    11.     nFileSizeHigh As Long
    12.     nFileSizeLow As Long
    13.     dwReserved0 As Long
    14.     dwReserved1 As Long
    15.     cFileName As String * 250
    16.     cAlternate As String * 14
    17. End Type
    18. Public Type SECURITY_ATTRIBUTES
    19.     nLength As Long
    20.     lpSecurityDescriptor As Long
    21.     bInheritHandle As Long
    22. End Type
    23. Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    24. Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    25. Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    26. Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    27. Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
    28.  
    29. Public Sub copyRoot(strSource As String, Optional strDest As String)
    30. Dim lngCntr As Long, lngHandle As Long, lngNextFile As Long
    31. Dim WFDFind As WIN32_FIND_DATA, SECAT As SECURITY_ATTRIBUTES
    32. Dim strFileName As String
    33. lngNextFile = 1
    34. lngHandle = FindFirstFile(strSource & "\*.*", WFDFind)
    35. While (lngNextFile)
    36.     strFileName = stripNull(WFDFind.cFileName)
    37.     If Not (strFileName = "." Or strFileName = "..") Then
    38.         If (GetFileAttributes(strSource & "\" & strFileName) And FILE_ATTRIBUTE_DIRECTORY) Then
    39.         'File attribute is DIRECTORY
    40.             'Create destination directory
    41.             lngTemp = CreateDirectory(strDest & "\" & strFileName, SECAT)
    42.             'Check here for desired folder name
    43.             Call copyRoot(strSource & "\" & strFileName, strDest & "\" & strFileName)              
    44.         Else
    45.             'Check here for desired filename
    46.             lngTemp = CopyFile(strSource & "\" & strFileName, strDest & "\" & strFileName, 0)            
    47.             DoEvents
    48.         End If
    49.     End If
    50.     lngNextFile = FindNextFile(lngHandle, WFDFind)
    51. Wend
    52. lngHandle = FindClose(lngHandle)
    53. End Sub
    Actually I made it for copying recursively. You can modify it for searching file.

  4. #4
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    But this doesn't searches the sub directory's

  5. #5
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    There is a project that i did just for fun. It's a file search engine.
    Look in the code you'll find that you need
    Attached Files Attached Files

  6. #6
    New Member
    Join Date
    Sep 2003
    Posts
    7
    I am trying to use the example that peet posted but I get "Compile Error: User-defined type not available." for all the variables at the top.

    Previously I have used:

    Dim FileSystemObject As Object
    Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

    for the FileSystemObject but don't know what to do for the Folder & File objects.

    I am really new to this so any help would be appreciated.

    Thanks.

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Hi Angela,
    in order to use the FSO in early binding mode, you have to add a
    reference to the 'Microsoft Scripting Runtime' (scrrun.dll)

    In vb, select from the menu : Project -> Add references...

    In the reference list, browse down until you find the 'Microsoft
    Scripting Runtime' (scrrun.dll)

    Make sure it is selected.

    Close the dialog.
    -= a peet post =-

  8. #8
    New Member
    Join Date
    Sep 2003
    Posts
    7
    Thanks peet, it worked!

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