Sure, I understand that you want the data listed hierarchically with each folder and its contents, including subfolders, and have folders and subfolders' names in bold font. Let's adjust the code accordingly.
Part 1: List Contents Hierarchically
Here's the function to list out the folder contents hierarchically, with folder and subfolder names in bold:
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ' Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ' Turn error handling back on
If CreateOrRetrieveSheet Is Nothing Then
' Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
' Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet("LISTA")
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ' Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(FolderPath)
' List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & "\"
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
' List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> ".tmp" Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Part 2: Verify Changes and Update the LISTA Sheet and List Changes in MODIFICARI
Here's the code that checks for changes and updates LISTA, and lists the changes in MODIFICARI:
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
wsChanges.Cells.Clear
' Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = "" Then
MsgBox "No folder path found in LISTA! Please run the listing first.", vbExclamation
Exit Sub
End If
' Populate dictionary with initial listing
For Each cell In wsLISTA.Range("A1:A" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
' Populate dictionary with current listing
For Each cell In wsTemp.Range("A1:A" & wsTemp.Cells(wsTemp.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = "Path"
wsChanges.Cells(rowChanges, 2).Value = "Change Type"
wsChanges.Cells(rowChanges, 3).Value = "Last Modified Date"
' Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Deleted"
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
' Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ' New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Added"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ' Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Modified"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
' Update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Complete Code with Hierarchical Listing and Change Monitoring
Here is the complete VBA code for both listing contents and monitoring changes:
Function CreateOrRetrieveSheet(sheetName As String) As Worksheet
On Error Resume Next ' Ignore errors while attempting to get sheet
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0 ' Turn error handling back on
If CreateOrRetrieveSheet Is Nothing Then
' Sheet does not exist, create it
Set CreateOrRetrieveSheet = ThisWorkbook.Sheets.Add
CreateOrRetrieveSheet.Name = sheetName
Else
' Clear existing content
CreateOrRetrieveSheet.Cells.Clear
End If
End Function
Sub ListFolderContentToSheet()
Dim ws As Worksheet
Set ws = CreateOrRetrieveSheet("LISTA")
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
ws.Cells(1, 4).Value = sItem ' Store the folder path in cell D1
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical sItem, ws, rowNum, 0
End Sub
Sub GetFolderContentHierarchical(ByVal FolderPath As String, ByVal ws As Worksheet, ByRef rowNum As Long, ByVal level As Integer)
Dim FSO As Object
Dim ParentFolder As Object
Dim FolderItem As Object
Dim FileItem As Object
Dim currentRow As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ParentFolder = FSO.GetFolder(FolderPath)
' List the folder itself
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & ParentFolder.Path & "\"
ws.Cells(rowNum, 1).Font.Bold = True
rowNum = rowNum + 1
' List the items in the folder
For Each FolderItem In ParentFolder.SubFolders
GetFolderContentHierarchical FolderItem.Path, ws, rowNum, level + 1
Next FolderItem
For Each FileItem In ParentFolder.Files
If Right(FileItem.Name, 4) <> ".tmp" Then
ws.Cells(rowNum, 1).Value = String(level * 2, " ") & FileItem.Path
ws.Cells(rowNum, 2).Value = FileItem.DateLastModified
rowNum = rowNum + 1
End If
Next FileItem
End Sub
Sub MonitorFolderAndUpdateChanges()
Dim wsLISTA As Worksheet
Dim wsChanges As Worksheet
Dim dictOriginal As Object
Dim dictCurrent As Object
Dim folderPath As String
Dim cell As Range
Set wsLISTA = ThisWorkbook.Sheets("LISTA")
Set wsChanges = CreateOrRetrieveSheet("MODIFICARI")
Set dictOriginal = CreateObject("Scripting.Dictionary")
Set dictCurrent = CreateObject("Scripting.Dictionary")
wsChanges.Cells.Clear
' Get folder path from sheet LISTA cell D1
folderPath = wsLISTA.Cells(1, 4).Value
If folderPath = "" Then
MsgBox "No folder path found in LISTA! Please run the listing first.", vbExclamation
Exit Sub
End If
' Populate dictionary with initial listing
For Each cell In wsLISTA.Range("A1:A" & wsLISTA.Cells(wsLISTA.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictOriginal(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
' List current folder content in a temp worksheet
Dim wsTemp As Worksheet
Set wsTemp = CreateOrRetrieveSheet("TempListing")
Dim rowNum As Long
rowNum = 1
GetFolderContentHierarchical folderPath, wsTemp, rowNum, 0
' Populate dictionary with current listing
For Each cell In wsTemp.Range("A1:A" & wsTemp.Cells(wsTemp.Rows.Count, "A").End(xlUp).Row)
If cell.Value <> "" And Right(cell.Value, 1) <> "\" Then
dictCurrent(cell.Value) = cell.Offset(0, 1).Value
End If
Next cell
Dim rowChanges As Long
rowChanges = 1
wsChanges.Cells(rowChanges, 1).Value = "Path"
wsChanges.Cells(rowChanges, 2).Value = "Change Type"
wsChanges.Cells(rowChanges, 3).Value = "Last Modified Date"
' Find deleted files/folders
For Each key In dictOriginal.Keys
If Not dictCurrent.Exists(key) Then
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Deleted"
wsChanges.Cells(rowChanges, 3).Value = dictOriginal(key)
End If
Next key
' Find added and modified files/folders
For Each key In dictCurrent.Keys
If Not dictOriginal.Exists(key) Then ' New file/folder
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Added"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
ElseIf dictCurrent(key) <> dictOriginal(key) Then ' Modified
rowChanges = rowChanges + 1
wsChanges.Cells(rowChanges, 1).Value = key
wsChanges.Cells(rowChanges, 2).Value = "Modified"
wsChanges.Cells(rowChanges, 3).Value = dictCurrent(key)
End If
Next key
' Update LISTA sheet with current data
wsLISTA.Cells.Clear
wsLISTA.Cells(1, 4).Value = folderPath
wsTemp.UsedRange.Copy wsLISTA.Cells(1, 1)
' Delete the temporary worksheet
Application.DisplayAlerts = False
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
Usage
To list the contents of the folder:
- Run the
ListFolderContentToSheet subroutine.
- This will open a folder picker dialog where you can select the folder whose contents you want to list.
- The contents will be listed hierarchically in the
LISTA sheet with folders and subfolders' names in bold.
To monitor for changes and update the LISTA sheet:
- Run the
MonitorFolderAndUpdateChanges subroutine.
- This will compare the current state of the folder with the state listed in the
LISTA sheet.
- Any changes (added, deleted, or modified files) will be listed in the
MODIFICARI sheet.
- The
LISTA sheet will also be updated to reflect the current state of the folder.