|
你看看是否可行:
'左边是文件路径,右边是文件大小(KB)
Option Explicit
Sub ListFilesInFolderAndSubfolders()
Dim FolderPath As String
Dim ws As Worksheet
' Prompt user to select a folder
FolderPath = GetFolderPath()
If FolderPath = "" Then Exit Sub
' Create a new worksheet to store file paths and sizes
Set ws = ThisWorkbook.Worksheets.Add
ws.Cells(1, 1).Value = "File Path"
ws.Cells(1, 2).Value = "File Size (KB)"
' Call the recursive function to list all files
Call ProcessFolder(FolderPath, ws)
' Autofit columns
ws.Columns("A:B").AutoFit
End Sub
Function GetFolderPath() As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.AllowMultiSelect = False
If .Show = -1 Then
GetFolderPath = .SelectedItems(1)
End If
End With
End Function
Sub ProcessFolder(FolderPath As String, ws As Worksheet)
Dim FSO As Object
Dim Folder As Object
Dim SubFolder As Object
Dim File As Object
Dim NextRow As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder(FolderPath)
' Process all files in the folder
For Each File In Folder.Files
NextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(NextRow, 1).Value = File.Path
ws.Cells(NextRow, 2).Value = File.Size / 1024
Next File
' Recursively process all subfolders
For Each SubFolder In Folder.SubFolders
Call ProcessFolder(SubFolder.Path, ws)
Next SubFolder
End Sub
|
评分
-
1
查看全部评分
-
|