Excel VBA和文件夹-1.10获取文件夹中文件的属性-即用型

前景提要

今天我们继续分享一些和文件夹相关的内容的操作,在日常的工作中,我们经常需要得到一个文件夹的属性,这样方便我们对文件夹进行分类,那么如何实现这个效果呢。直接上代码,

上代码

看起来是密密麻麻的挺多内容的,对于不是很熟悉VBA的童鞋来说,下面的代码是非常难懂的,确实是,这里面涉及的东西很多,有API,FSO等方面的知识点,不过我们这里既然已经强调了是即用型,就是说我们只需要直接套用代码就可以,稍微更改下文件夹的位置就可以得到我们想要的效果了,大家可以收藏起来,说不定哪一天会使用到的。

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 & "创建时间:" & 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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容