2021年12月6日 星期一

VBA_建立檔案目錄

使用VBA抓取檔案路徑中的清單,用在交接把自己手上檔案移交給人時很好用,當然你從人家手上接到檔案,如果沒清單時也很好用。

Sub A0_檔案清單_DIC()
Application.ScreenUpdating = False
' 比ARR慢一點點
t = Timer
Dim DIC
Set DIC = CreateObject("scripting.dictionary")

Range("A:L").ClearContents
'n = 0
Range("A1").Resize(, 8) = Array("檔名", "路徑", "大小", "修改時間", "建立時間", "授權時間", "資料夾", "檔案格式")

Dim strPath As String
strPath = "D:\稽核工作" '修改這裡

Dim OBJ As Object, Folder As Object, File As Object

Set OBJ = CreateObject("Scripting.FileSystemObject")
Set Folder = OBJ.GetFolder(strPath)

Call ListDIC(Folder, DIC)

Dim SubFolder As Object

For Each SubFolder In Folder.SubFolders '子資料夾
    Call ListDIC(SubFolder, DIC)
    Call GetSubFolders(SubFolder, DIC)
Next SubFolder

Range("A2").Resize(DIC.Count, 8) = Application.Transpose(Application.Transpose(DIC.Items))
MsgBox Timer - t

DIC.RemoveAll

End Sub
'---------------------------------------------------------------------------------------------------------
Sub ListDIC(ByRef Folder As Object, DIC)

For Each File In Folder.Files'擷取的內容
        DIC(File.Path) = Array(File.Name, File.Path, (File.Size / 1024), File.DateLastModified, _
            File.DateCreated, File.DateLastAccessed, Split(File.ShortPath, "\" & File.Name)(0), File.Type)
Next File

End Sub
'------------------------------------------------------------------------
Sub GetSubFolders(ByRef SubFolder As Object, DIC)

Dim FolderItem As Object '資料夾

For Each FolderItem In SubFolder.SubFolders
    Call ListDIC(FolderItem, DIC)
    Call GetSubFolders(FolderItem, DIC)   '這個方式沒使用過
Next FolderItem

End Sub

沒有留言:

張貼留言

用GPT修改M語法_以日期列處理

在2023年GPT開始使用時我就詢問過GPT,可能當時的GPT還不夠聰明,給予的答案無法運行,我認為原始的語法應該可以更聰明點,詢問過Power BI社群的人,但沒有人提供答案,終於在現在再度詢問GPT給予的答案與我思考的方向依樣,只是我懂得語法不多,透過GPT幫忙解答,我也學到...