List of files in folders - Update the current list

Formula11

Active Member
Joined
Mar 1, 2005
Messages
433
Office Version
  1. 365
Platform
  1. Windows
I have this macro which lists the Name and Title of all files in a folder and its sub-folders.
The file make-up in the folders and subfolders changes frequently. Files are added, deleted, renamed, re-titled, etc.
What I want to do is update the current output in Excel based on the change events above:
(1) If a file is added, add a row in Excel and output Name and Title. The row needs to be added such that list is sequential … this is the difficult part.
(2) If a file is deleted, delete that row.
(3) If a file is renamed, carry out (1) and (2). Or not?
(4) If a file has the Title changed (assuming Name is the same), change output in the corresponding row. Could look at Date Modified for that file?
A similar principle would apply to subfolders. If there is a new one for example, the group output would be in sequential order with respect to other subfolders.
Is this possible?


Code:
Dim Row As Long

Sub File_Details()
    Dim sFolder As FileDialog
    On Error Resume Next
    Set sFolder = Application.FileDialog(msoFileDialogFolderPicker)
    If sFolder.Show = -1 Then
        Row = 0
        File_Details_List_Files sFolder.SelectedItems(1), True
    End If
End Sub

Private Sub File_Details_List_Files(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    'Declare Variables
    Dim FSO As Object
    Dim SourceFolder As Object
    Dim SubFolder As Object
    Dim FileItem As Object
    Dim strFile As String
    Dim FileName As Variant
    'Setup
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    Application.ScreenUpdating = False
    If Row = 0 Then Row = ActiveCell.Row
    'Start
    With CreateObject("Scripting.Dictionary")
    'Filenames
    For Each FileItem In SourceFolder.Files
        strFile = FileItem.Name
        .Item(strFile) = Array(FileItem.Name)
    Next FileItem
    If .Count > 0 Then
        For Each FileName In .Items
            Rows(Row).Insert
            Cells(Row, 3).Formula = FileName(LBound(FileName))
            Cells(Row, 4).Formula = Get_File_Detail_Title(SourceFolder.Path, FileName(LBound(FileName)))
            Row = Row + 1
        Next FileName
    End If
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.subfolders
            File_Details_List_Files SubFolder.Path, True
        Next SubFolder
    End If
    End With
    'End
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
    Application.ScreenUpdating = True
End Sub

Function Get_File_Detail_Title(ByVal FilePath As String, ByVal FileName As String)
    Dim objFolder As Object
    Dim objFolderItem As Object
    Dim objShell As Object
    FileName = StrConv(FileName, vbUnicode)
    FilePath = StrConv(FilePath, vbUnicode)
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(StrConv(FilePath, vbFromUnicode))
    If Not objFolder Is Nothing Then
        Set objFolderItem = objFolder.ParseName(StrConv(FileName, vbFromUnicode))
    End If
    If Not objFolderItem Is Nothing Then
        Get_File_Detail_Title = objFolder.GetDetailsOf(objFolderItem, 21) '10 (Windows xp), 21 (Windows Vista, Windows 7, Windows 8)
    Else
        Get_File_Detail_Title = ""
    End If
    Set objShell = Nothing
    Set objFolder = Nothing
    Set objFolderItem = Nothing
End Function
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
you would just erase the existing list, then import all names again.
This fixes deleted files, renamed files, etc.
 
Upvote 0
With respect to importing all over again, because I have a large number of files, many which are large PDFs, my system just can't cope and it freezes. At the moment I delete old data and do a portion at a time.
Hence the reason for trying to just update and not start again.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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