QooApp さん
MMYS さん
ご指摘ありがとうございます。
ダイアログでフォルダを指定し、FSO の再起処理を使用した
フォルダ/ファイル を ツリー表示するコードです。
ファイル名の変更は、Fileオブジェクトの Nameプロパティーを変える処理を入れてください。
https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/name-property-filesystemobject-object
どんな規則性でファイル名を変えるのか判りませんが正規表現を使うでしょうから Office TANAKA さんの
【正規表現によるマッチング】
http://officetanaka.net/excel/vba/tips/tips38.htm
を参考にファイル名変更処理を入れ込んでください。
-------------------------------------------------------------------------------------
Sub GetFolderTree()
On Error GoTo Err_GetFolderTree
Dim TargetPath As String
Dim strPath As String
Dim r As Long
Dim c As Long
Dim strMsg As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = False Then strMsg = "キャンセルされました": GoTo Exit_GetFolderTree
TargetPath = .SelectedItems(1)
End With
Cells.Delete
Set FSO = CreateObject("Scripting.FileSystemObject")
Call GetSubFolders(FSO.GetFolder(TargetPath), 1, 1)
strMsg = "終了"
Exit_GetFolderTree:
Set FSO = Nothing
MsgBox strMsg
Exit Sub
Err_GetFolderTree:
strMsg = Err.Number & vbCrLf & Err.Description
GoTo Exit_GetFolderTree
End Sub
Private Sub GetSubFolders(ByVal Fld As Folder, ByRef r As Long, ByVal c As Long)
Dim subFld As Scripting.Folder
r = r + 1
c = c + 1
Cells(r, 1).value = "Folder"
With Cells(r, c)
.Font.Italic = True
.value = Fld.Name
End With
For Each subFld In Fld.subFolders
Call GetSubFolders(subFld, r, c)
Next
Call GetFiles(Fld, r, c)
Set subFld = Nothing
End Sub
Private Sub GetFiles(ByVal Fld As Folder, ByRef r As Long, ByVal c As Long)
Dim Fil As Scripting.File
c = c + 1
r = r + 1
For Each Fil In Fld.Files
Cells(r, 1).value = "File"
Cells(r, c).value = Fil.Name
r = r + 1
Next
End Sub
-------------------------------------------------------------------------------------