前景提要
今天我們繼續(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