VBA分享专栏

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

2019-06-19  本文已影响1人  Excel和VBA

前景提要

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

上代码

看起来是密密麻麻的挺多内容的,对于不是很熟悉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
上一篇下一篇

猜你喜欢

热点阅读