Excel VBA和文件夾-1.10獲取文件夾中文件的屬性-即用型

前景提要

今天我們繼續(xù)分享一些和文件夾相關的內(nèi)容的操作,在日常的工作中,我們經(jīng)常需要得到一個文件夾的屬性,這樣方便我們對文件夾進行分類,那么如何實現(xiàn)這個效果呢。直接上代碼,

上代碼

看起來是密密麻麻的挺多內(nèi)容的,對于不是很熟悉VBA的童鞋來說,下面的代碼是非常難懂的,確實是,這里面涉及的東西很多,有API,FSO等方面的知識點,不過我們這里既然已經(jīng)強調(diào)了是即用型,就是說我們只需要直接套用代碼就可以,稍微更改下文件夾的位置就可以得到我們想要的效果了,大家可以收藏起來,說不定哪一天會使用到的。

Const FileAttrNormal = 0

Const FileAttrReadOnly = 1

Const FileAttrHidden = 2

Const FileAttrSystem = 4

Const FileAttrVolume = 8

Const FileAttrDirectory = 16

Const FileAttrArchive = 32

Const FileAttrAlias = 64

Const FileAttrCompressed = 128

Sub FileFunc()

    Dim fso, folder, fc, f1

    Dim strTmp As String

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder("換成你想要的路徑")'更換下路徑

    Set fc = folder.Files

    For Each f1 In fc

        strTmp = strTmp & f1.name & "的詳細資料:" & vbCrLf

        strTmp = strTmp & vbTab & "路徑:" & f1.Path & vbCrLf

        strTmp = strTmp & vbTab & "類型:" & f1.Type & vbCrLf

        strTmp = strTmp & vbTab & "創(chuàng)建時間:" & f1.DateCreated & vbCrLf

        strTmp = strTmp & vbTab & "最后訪問時間:" & f1.DateLastAccessed & vbCrLf

        strTmp = strTmp & vbTab & "最后修改時間:" & f1.DateLastModified & vbCrLf

        strTmp = strTmp & vbTab & "文件大小(Bytes):" & f1.Size & vbCrLf

    Next

    MsgBox strTmp

End Sub

Function GetFileAttr(ObjFile) As String

    Dim strTmp As String

    Dim attr

    attr = ObjFile.Attributes

    If attr = 0 Then

      GetFileAttr = "Normal"

      Exit Function

   End If

   If attr And FileAttrDirectory Then strTmp = strTmp & "Directory "

   If attr And FileAttrReadOnly Then strTmp = strTmp & "Read-Only "

   If attr And FileAttrHidden Then strTmp = strTmp & "Hidden "

   If attr And FileAttrSystem Then strTmp = strTmp & "System "

   If attr And FileAttrVolume Then strTmp = strTmp & "Volume "

   If attr And FileAttrArchive Then strTmp = strTmp & "Archive "

   If attr And FileAttrAlias Then strTmp = strTmp & "Alias "

   If attr And FileAttrCompressed Then strTmp = strTmp & "Compressed "

   GetFileAttr = strTmp

End Function

效果如圖:


1.jpg
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容