+ Reply to Thread
Results 1 to 8 of 8

FSO to search until subfolders

Hybrid View

  1. #1
    Registered User
    Join Date
    05-21-2013
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    23

    Question FSO to search until subfolders

    Hi experts!

    I just copied this code;
    does anybody have an idea on how i can change it
    to search til sub folders

    Sub file_attrib1()
        Dim fso As New FileSystemObject
        Dim fls As Files
        Dim strText As String
        Dim i As Integer
        
        
        Set fls = fso.GetFolder("D:\BackUp").Files
        
        i = 2
        
            For Each f In fls
                .Cells(i, 1) = f.Name
                .Cells(i, 2) = f.Size
                .Cells(i, 3) = f.DateCreated
                .Cells(i, 4) = f.DateLastModified
                .Cells(i, 5) = f.Type
                .Cells(i, 6) = f.Path
               
                i = i + 1
            Next
        End With
    End Sub
    Thanks in advance

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,922

    Re: FSO to search until subfolders

    What does "...to search til sub folders..." mean?
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    05-21-2013
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: FSO to search until subfolders

    hi protonLeah
    thank you very much for your reply.
    i run this code and it only list files within the folder "D:\BackUp" it does not display files inside back up's sub folder

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: FSO to search until subfolders

    Try this:

    Option Explicit
    
    Sub FilesInFolderInfo()
      Dim myFolder$
      ' Pick folder
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select a folder to list Files from"
            .InitialFileName = "C:\Temp\Old Folder"                         '<<< Change here
            .Show
            If .SelectedItems.Count > 0 Then myFolder = .SelectedItems(1) & "\" Else Exit Sub
        End With
      ' Loop through all subfolders
        ListFilesInFolder myFolder, True
    End Sub
    
    Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
      Dim r&, fso As Object, SourceFolder, FileItem, SubFolder
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = fso.GetFolder(SourceFolderName)
            r = 1
            For Each FileItem In SourceFolder.Files
              With FileItem
                r = r + 1
                Cells(r, 1) = .Name
                Cells(r, 2) = .Size
                Cells(r, 3) = .DateCreated
                Cells(r, 4) = .DateLastModified
                Cells(r, 5) = .Type
                Cells(r, 6) = .Path
              End With
            Next
                If IncludeSubfolders Then
                    For Each SubFolder In SourceFolder.SubFolders
                        ListFilesInFolder SubFolder.Path, True
                    Next SubFolder
                End If
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set fso = Nothing
    End Sub
    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Registered User
    Join Date
    05-21-2013
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: FSO to search until subfolders

    thank you very much abousetta

    but sorry im just a newbie with excel macro
    can we omit the portion of the opening of dialog box?

  6. #6
    Registered User
    Join Date
    05-21-2013
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: FSO to search until subfolders

    thank you very much abousetta

    but sorry im just a newbie with excel macro
    can we omit the portion of the opening of dialog box?

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: FSO to search until subfolders

    Just replace:
      ' Pick folder
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = Application.DefaultFilePath & "\"
            .Title = "Please select a folder to list Files from"
            .InitialFileName = "C:\Temp\Old Folder"                         '<<< Change here
            .Show
            If .SelectedItems.Count > 0 Then myFolder = .SelectedItems(1) & "\" Else Exit Sub
        End With
    with
      ' Pick folder
        myFolder = "C:\Temp\Old Folder\"                         '<<< Change here
    abousetta

  8. #8
    Registered User
    Join Date
    05-21-2013
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: FSO to search until subfolders

    you are sooo great abousetta
    Thank you very much!!!!!!!!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to search a file in folder and subfolders
    By rakeshredround in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2012, 03:35 AM
  2. [SOLVED] Search subfolders in excel 2010
    By greasybob in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-08-2012, 07:03 PM
  3. Workbooks.open to search in a certain path and its subfolders
    By Macko07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2012, 08:12 AM
  4. Replies: 0
    Last Post: 03-05-2009, 01:43 PM
  5. file search in subfolders
    By Pflugs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2005, 12:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1