类模块 mFile
用于文件和文件夹的查询和操作。
版本号 Version:1.2
最终更新日期 Last Update Date:2022-05-26
作者 Author:JunYi
网址 Link:https://gitee.com/junyii/vba-code-base
更新内容 Updates 1.2 2022-05-23:
更新内容 Updates 1.1 2022-05-06:
全面革新:
一年多以来,mFile
很好得解决了文件与文件夹相关的问题。然而,随着需求的变多,这些方法并不能满足所有,需要改进。以下:
更新内容 Updates 1.0 2021-04-03
更新内容 Updates 0.6 2022-03-26:
更新内容 Updates 0.5 2022-03-26:
更新内容 Updates 2021-01-21:
更新内容 Updates 2021-01-15:
更新内容 Updates 2021-01-09:
更新内容 Updates 2020-12-26:
更新内容 Updates 2020-12-23:
类模块 mFile
用于文件和文件夹的查询和操作。
下边是一些在测试时,需要在外部定义的公共变量和对象。
Option Explicit
Dim mFile As New mFile
Dim Str As String
下边是一些测试中用到的自定义函数和公共常量。
Public Const EngMode As Boolean = True ' 英文模式
Public Const mName As String = "JunYi Code Base"
' 坏消息
Public Function BadMsg(Message As String)
MsgBox Message, vbCritical, mName
End Function
' 好消息
Public Function GoodMsg(Message As String)
MsgBox Message, vbInformation, mName
End Function
获得某路径下的所有文件夹。由 Property Get
创建,通过递归的方法获得文件夹名称。返回一个字符串 SubFolder1,SubFolder2, ...
,可以通过 Split
函数将结果拆分(分隔符默认是英文状态的逗号)。
Property Get GetSubFolders(mPath As String, Optional SubLevel As Integer = 0, Optional mKeyWord As String = "", Optional mSeparator As String = ",") As String
,
。Sub File_GetSubFolder()
Dim mFolderName() As String
Dim i As Integer
mFolderName = Split(mFile.GetSubFolders("D:\迅雷下载\test", 3, "1"), ",") ' 将文件名拆分到数组
Sheets("File").Range("E:E").Clear
For i = LBound(mFolderName) To UBound(mFolderName)
Str = Str & i & " - " & mFolderName(i) & vbCrLf
'Sheets("File").Cells(i + 1, 5) = mFolderName(i)
Next i
GoodMsg Replace(Str, "D:\迅雷下载\test", "")
Str = ""
End Sub
执行结果: ![[Pasted image 20201226122010.png]]
Dialog获取文件夹。由 Property Get
创建,返回一个字符串。
Property Get GetFolderDialog() As String
Sub File_GetFolderDialog()
GoodMsg mFile.GetFolderDialog
End Sub
执行结果: ![[Pasted image 20210121143652.png]] ![[Pasted image 20210121143701.png]]
检查文件夹是否存在。由 Property Get
创建,返回一个布尔值。
Property Get CheckFolderExists(mPath As String) As Boolean
Sub File_CheckFolderExists()
Str = ThisWorkbook.path & "\测试"
GoodMsg mFile.CheckFolderExists(Str)
End Sub
创建文件夹。
Sub CreateFolder(mFolderPath As String, Optional ShowMsg As Boolean = False)
False
不提示。Sub File_CreateFolder()
Str = ThisWorkbook.path & "\测试\test\"
mFile.CreateFolder Str, True
End Sub
获得文件名称。由 Property Get
创建。
FileName1,FileName2, ...
,可以通过 Split
函数将结果拆分(分隔符默认是英文状态的逗号)。Property Get GetFileName(mPath As String, mKeyWord As String, Optional mFileExtention As String = "*", Optional mSeparator As String = ",") As String
*
,即:查找所有类型的文件。,
。Sub File_GetFileName()
Dim mFileName() As String
Dim i As Integer
mFileName = Split(mFile.GetFileName("D:\迅雷下载\", "HD", "mkv", "&&"), "&&") ' 将文件名拆分到数组
For i = LBound(mFileName) To UBound(mFileName)
Str = Str & i & " - " & mFileName(i) & vbCrLf
Next i
GoodMsg Str
Str = ""
End Sub
执行结果:
![[Pasted image 20201226122227.png]]
检测文件是否存在。由 Property Get
创建,返回一个布尔值。
Property Get CheckFileExists(mPath As String) As Boolean
Sub File_CheckFileExists()
Dim CheckResult As Boolean
Str = ThisWorkbook.path & "\测试.xlsx"
CheckResult = mFile.CheckFileExists(Str)
If CheckResult Then GoodMsg "文件存在:" & Str
If Not CheckResult Then BadMsg "文件不存在:" & Str
Str = ""
End Sub
执行结果:
![[Pasted image 20201226123647.png]]
读取 ini 文件。由 Property Get
创建,返回一个字符串。
Property Get ReadIni(FileFullName As String, SectionName As String, KeyName As String) As String
Sub File_ReadIni()
Dim mPath As String
mPath = ThisWorkbook.path & "\test.ini"
GoodMsg mFile.ReadIni(mPath, "Test", "test1")
End Sub
读取 ini 文件下整个Section数据。由 Property Get
创建,返回一个字符串。
Property Get ReadIniSection(FileFullName As String, SectionName As String, Optional mSeparator As String = "&&") As String
Sub File_ReadIniSection()
Dim mPath As String
mPath = ThisWorkbook.path & "\测试\test.ini"
GoodMsg mFile.ReadIniSection(mPath, "Test")
End Sub
写入 ini文件。
Sub WriteIni(FileFullName As String, SectionName As String, KeyName As String, KeyValue As String)
示例:
Sub Test()
mFile.WriteIni ThisWorkbook.Path & "\settings.ini", "test2", "testkey", "哈哈~"
GoodMsg mFile.ReadIni(ThisWorkbook.Path & "\settings.ini", "test2", "testkey")
End Sub
![[Pasted image 20220326164800.png]]
删除 ini 键值。
Sub DeleteIniKey(FileFullName As String, SectionName As String, KeyName As String)
示例:
Sub File_DeleteIni()
Dim mPath As String
mPath = ThisWorkbook.path & "\测试\test.ini"
mFile.DeleteIniKey mPath, "Test", "test1"
GoodMsg "删除成功。"
End Sub
创建文件。
Sub CreateFile(mFileFullPath As String, Optional ShowMsg As Boolean = False)
False
不提示。Sub File_CreateFile()
Dim mPath As String
mPath = ThisWorkbook.path & "\测试\test.ini"
mFile.CreateFile mPath, True
End Sub
复制文件。返回一个是否复制成功的布尔值。
Function CopyFile(mSourceFilePath As String, mDestinyFilePath As String, Optional mShowMsg As Boolean = True)
True
提示。Sub File_CopyFile()
Dim mPath As String, mPathAfter As String
mPath = ThisWorkbook.path & "\测试\test.ini"
mPathAfter = ThisWorkbook.path & "\测试\test~~.ini"
mFile.CopyFile mPath, mPathAfter, False
GoodMsg "Done"
End Sub
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。