+ Reply to Thread
Results 1 to 6 of 6

Selective listing of files in a folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Selective listing of files in a folder

    The following code lists all files in a selected folder. Is there any way to list only files that contain specific text. In my case I want to only list files with both brackets. From the list below, output is Volume 1 (Section 1) and Volume 3 (S7). Text within brackets maybe of any length.
    I would like to keep code in the same format.

    Volume 1 (Section 1)
    Volume 2 All
    Volume 3 (S7)
    Volume 4, Section 10
    Volume 5 - Section 3


    Sub File_Attributes()
        Dim sFolder As FileDialog
        Set sFolder = Application.FileDialog(msoFileDialogFolderPicker)
        If sFolder.Show = -1 Then
        File_Attributes_List_Files sFolder.SelectedItems(1), True
        End If
    End Sub
    
    Sub File_Attributes_List_Files(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
        Dim FSO As Object
        Dim SourceFolder As Object
        Dim SubFolder As Object
        Dim FileItem As Object
        Dim r As Long
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        r = ActiveCell.Row
        For Each FileItem In SourceFolder.Files
        Rows(r).Insert
        Cells(r, 3).Formula = FileItem.Name
        r = r + 1
        x = SourceFolder.Path
        Next FileItem
        If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
        File_Attributes_List_Files SubFolder.Path, True
        Next SubFolder
        End If
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Selective listing of files in a folder

    Perhaps.
    Sub File_Attributes()
    Dim sFolder As FileDialog
        Set sFolder = Application.FileDialog(msoFileDialogFolderPicker)
        If sFolder.Show = -1 Then
            File_Attributes_List_Files sFolder.SelectedItems(1), True
        End If
    End Sub
    
    Sub File_Attributes_List_Files(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim SubFolder As Object
    Dim FileItem As Object
    Dim r As Long
    
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
    
        r = ActiveCell.Row
    
        For Each FileItem In SourceFolder.Files
            If FileItem.Name Like "*(*)*" Then
                Rows(r).Insert
                Cells(r, 3).Formula = FileItem.Name
                r = r + 1
            End If
        Next FileItem
    
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                File_Attributes_List_Files SubFolder.Path, True
            Next SubFolder
        End If
    
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Selective listing of files in a folder

    If I replace with square brackets, it does not work. Do you know why?

            If FileItem.Name Like "*[*]*" Then

  4. #4
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Selective listing of files in a folder

    It seems to work.
    Thanks for the modification.

  5. #5
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Selective listing of files in a folder

    Thank you again.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Selective listing of files in a folder

    When using the Like operator anything between square brackets is treated as a character list to match a single character.

    For example if you wanted to check for A-Z you would use [A-Z].

    Try this for finding [ followed by ].
    Sub File_Attributes()
    Dim sFolder As FileDialog
        Set sFolder = Application.FileDialog(msoFileDialogFolderPicker)
        If sFolder.Show = -1 Then
            File_Attributes_List_Files sFolder.SelectedItems(1), True
        End If
    End Sub
    
    Sub File_Attributes_List_Files(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim SubFolder As Object
    Dim FileItem As Object
    Dim r As Long
    Dim pos1 As Long
    Dim pos2 As Long
    
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
    
        r = ActiveCell.Row
    
        For Each FileItem In SourceFolder.Files
        
            pos1 = InStr(FileItem.Name, "[")
    
            If pos1 > 0 Then
    
                pos2 = InStr(FileItem.Name, "]")
    
                If pos2 > pos1 Then
                    Rows(r).Insert
                    Cells(r, 3).Formula = FileItem.Name
                    r = r + 1
                End If
                
            End If
            
        Next FileItem
    
        If IncludeSubfolders Then
            For Each SubFolder In SourceFolder.SubFolders
                File_Attributes_List_Files SubFolder.Path, True
            Next SubFolder
        End If
    
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        ActiveWorkbook.Saved = True
    
    End Sub

+ 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. listing excel files in a folder including subfolders
    By olivermdantes in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 02-22-2016, 04:17 AM
  2. Listing number of files per folder?
    By dark91zc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-07-2013, 03:20 PM
  3. Replies: 0
    Last Post: 06-17-2013, 05:33 AM
  4. [SOLVED] VBA for listing Products in All Excel Files in Folder
    By e.isayev in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-16-2013, 05:26 PM
  5. Listing/Renaming Files in a folder from excel
    By Kelshaer in forum Excel Tips
    Replies: 4
    Last Post: 01-23-2013, 11:04 AM
  6. Listing files contained in a folder
    By boylejob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-04-2008, 11:13 AM
  7. Periodically listing files in a folder
    By haven104 in forum Excel General
    Replies: 2
    Last Post: 01-10-2006, 05:10 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