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?
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