当前位置:首页> 正文

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

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
Exit Function
End If
If attr Next 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

效果如图:

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

==========================================================================

因为这次分享的主要是即用型的代码,拿来就可以使用的,没有太多的知识点需要说明的,大家有了前面的基础,基本上都可以看懂和理解代码的意思的,所以后面我还打算通过这样的方式分享2篇左右关于文件夹的一些实用的即用型代码,或者你有什么其他的需求,也可以在下面留言告诉我,我尽量根据大家的需要分享相关的知识。

说下计划吧,关于文件夹的一些知识和内容的分享,大致快要收尾了,后面将会进入关于VBA中关于FSO的一些内容的分享

好啦,今天的分享就到这里了,如果你有什么想要分享的,或者有什么想要知道的,都可以在留言,明天晚上19:00再见!

==========================================================================

本人已经打算长期分享一些网络搜集的各种VBA知识,如果大家有问题也可以提出来共同解决,一起进步,毕竟VBA的世界还是很大的。

因为基本上都是自学的,很多代码和知识都是来源于网络的,如果在分享的过程中,正好拿了某位大神的代码,请告诉我,我将标注代码来源出处,也方便大家学习分享,谢谢!

=============================传送门==============================

Excel VBA和文件夹-1.7通过对话框灵活选定文件夹并打开对应文件

Excel VBA和文件夹-1.8通过对话框灵活选定文件的小技巧

Excel VBA和文件夹-1.9获得文件的最后修改时间-即用型

展开全文阅读

相关内容