Creating a list of file names

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi all, I hope you might be able to give me a few pointers on this one.

I have a folder that contains loads of emails. What I would like to do is create an index in excel that list all the file names.

Is this possible?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
thanks for the reply

thanks for the reply unfortuatley, my works system has blocked the address. Are there any other links or could some kind sole paste the vba?
 
Upvote 0
Try this. I think it requires you to navigate to the desired folder via File-Open before running the macro:

Code:
Function CreateFileList(FileFilter As String, _
    IncludeSubFolder As Boolean) As Variant
' returns the full filename for files matching
' the filter criteria in the current folder
Dim FileList() As String, FileCount As Long
    CreateFileList = ""
    Erase FileList
    If FileFilter = "" Then FileFilter = "*.*" ' all files
    With Application.FileSearch
        .NewSearch
        .LookIn = CurDir
        .Filename = FileFilter
        .SearchSubFolders = IncludeSubFolder
        .FileType = msoFileTypeAllFiles
        If .Execute(SortBy:=msoSortByFileName, _
            SortOrder:=msoSortOrderAscending) = 0 Then Exit Function
        ReDim FileList(.FoundFiles.Count)
        For FileCount = 1 To .FoundFiles.Count
            FileList(FileCount) = .FoundFiles(FileCount)
        Next FileCount
        .FileType = msoFileTypeExcelWorkbooks ' reset filetypes
    End With
    CreateFileList = FileList
    Erase FileList
End Function

Sub TestCreateFileList()
Dim FileNamesList As Variant, i As Integer
     ' activate the desired startfolder for the filesearch
    FileNamesList = CreateFileList("*.*", False)
    ' performs the filesearch, includes any subfolders
    ' present the result
    Range("A:A").ClearContents
    For i = 1 To UBound(FileNamesList)
        Cells(i + 1, 1).Formula = FileNamesList(i)
    Next i
End Sub
 
Upvote 0
Try to use file search


Code:
Sub List_Files() 
Dim fs As FileSearch, i As Long 
Set fs = Application.FileSearch 
With fs 
    .LookIn = "ur path" 
    .FileType = msoFileTypeExcelWorkbooks 
    If .Execute > 0 Then 
        For i = 1 To .FoundFiles.Count 
             Range("A" & i).value = .Filename            
        Next 
    End If 
End With 
End Sub
 
Upvote 0
Re: thanks for the reply

thanks for the reply unfortuatley, my works system has blocked the address. Are there any other links or could some kind sole paste the vba?

Here is the code

Code:
Option Explicit

Private cnt As Long
Private arfiles
Private level As Long

Sub Folders()
Dim i As Long
Dim sFolder As String
Dim iStart As Long
Dim iEnd As Long
Dim fOutline As Boolean

    arfiles = Array()
    cnt = -1
    level = 1

    sFolder = "E:\"
    ReDim arfiles(2, 0)
    If sFolder <> "" Then
        SelectFiles sFolder
        Application.DisplayAlerts = False
        On Error Resume Next
        Worksheets("Files").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
        Worksheets.Add.Name = "Files"
        With ActiveSheet
            For i = LBound(arfiles, 2) To UBound(arfiles, 2)
                If arfiles(0, i) = "" Then
                    If fOutline Then
                        Rows(iStart + 1 & ":" & iEnd).Rows.Group
                    End If
                    With .Cells(i + 1, arfiles(2, i))
                        .Value = arfiles(1, i)
                        .Font.Bold = True
                    End With
                    iStart = i + 1
                    iEnd = iStart
                    fOutline = False
                Else
                    .Hyperlinks.Add Anchor:=.Cells(i + 1, arfiles(2, i)), _
                                    Address:=arfiles(0, i), _
                                    TextToDisplay:=arfiles(1, i)
                    iEnd = iEnd + 1
                    fOutline = True
                End If
            Next
            .Columns("A:Z").ColumnWidth = 5
        End With
    End If
    'just in case there is another set to group
    If fOutline Then
        Rows(iStart + 1 & ":" & iEnd).Rows.Group
    End If

    Columns("A:Z").ColumnWidth = 5
    ActiveSheet.Outline.ShowLevels RowLevels:=1
    ActiveWindow.DisplayGridlines = False

End Sub

'-----------------------------------------------------------------------
Sub SelectFiles(Optional sPath As String)
'-----------------------------------------------------------------------
Static FSO As Object
Dim oSubFolder As Object
Dim oFolder As Object
Dim oFile As Object
Dim oFiles As Object
Dim arPath

    If FSO Is Nothing Then
        Set FSO = CreateObject("Scripting.FileSystemObject")
    End If

    If sPath = "" Then
        sPath = CurDir
    End If

    arPath = Split(sPath, "\")
    cnt = cnt + 1
    ReDim Preserve arfiles(2, cnt)
    arfiles(0, cnt) = ""
    arfiles(1, cnt) = arPath(level - 1)
    arfiles(2, cnt) = level

    Set oFolder = FSO.GetFolder(sPath)
    Set oFiles = oFolder.Files
    For Each oFile In oFiles
        cnt = cnt + 1
        ReDim Preserve arfiles(2, cnt)
        arfiles(0, cnt) = oFolder.Path & "\" & oFile.Name
        arfiles(1, cnt) = oFile.Name
        arfiles(2, cnt) = level + 1
    Next oFile

    level = level + 1
    For Each oSubFolder In oFolder.Subfolders
        SelectFiles oSubFolder.Path
    Next
    level = level - 1

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,380
Messages
6,130,274
Members
449,570
Latest member
TomMacca52

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top