Skip to content

cToolsFso 类

文件系统操作工具类,提供路径处理、目录管理等常用功能。

说明

cToolsFso 是对 Scripting.FileSystemObject 的封装,提供更便捷的文件路径处理和目录操作功能。内部通过 Inst 属性暴露原始的 FSO 对象,方便进行高级操作。

引用库:

  • Microsoft Scripting Runtime (scrrun.dll)

属性

属性名类型说明
InstScripting.FileSystemObjectFSO 实例对象,可直接访问原始 FSO 方法

方法

IsFullPath

判断路径是否为完整路径(包含盘符)。

语法:

vb
Public Function IsFullPath(Path As String) As Boolean

参数:

参数类型说明
PathString要检查的路径

返回值:

  • True - 是完整路径(包含 :\
  • False - 是相对路径

示例:

vb
Dim Fso As New cToolsFso

Debug.Print Fso.IsFullPath("C:\Windows\System32")  ' True
Debug.Print Fso.IsFullPath("..\data\file.txt")     ' False
Debug.Print Fso.IsFullPath("data\file.txt")         ' False

AutoCompleteFullPath

自动补全为完整路径,支持相对路径转绝对路径。

语法:

vb
Public Function AutoCompleteFullPath(Path As String, Optional IsFile As Boolean) As String

参数:

参数类型说明
PathString原始路径
IsFileBoolean可选。是否为文件路径(影响末尾斜杠处理)

返回值:

  • 完整路径字符串

说明:

  • 如果路径已是完整路径,则直接返回
  • 如果是相对路径,则在前面追加 App.Path
  • 自动清理路径中的多余斜杠

示例:

vb
Dim Fso As New cToolsFso

' 相对路径转绝对路径
Debug.Print Fso.AutoCompleteFullPath("data\config.ini")
' 输出: C:\Program Files\MyApp\data\config.ini

' 已是完整路径则保持不变
Debug.Print Fso.AutoCompleteFullPath("D:\\backup\\data.txt")
' 输出: D:\backup\data.txt

ClearSpan

清理路径中的斜杠,统一为反斜杠格式。

语法:

vb
Public Function ClearSpan(Path As String, Optional IsFile As Boolean) As String

参数:

参数类型说明
PathString要清理的路径
IsFileBoolean可选。是否为文件路径(目录会自动添加末尾斜杠)

返回值:

  • 清理后的路径字符串

说明:

  • //\// 统一替换为 \
  • 如果是目录路径,自动在末尾添加 \

示例:

vb
Dim Fso As New cToolsFso

' 统一斜杠格式
Debug.Print Fso.ClearSpan("C://Windows\\System32//file.txt", True)
' 输出: C:\Windows\System32\file.txt

' 目录路径自动添加末尾斜杠
Debug.Print Fso.ClearSpan("C:\\Windows\\System32", False)
' 输出: C:\Windows\System32\

AutoMakeDir

自动创建目录(如果不存在)。

语法:

vb
Public Function AutoMakeDir(ByVal Path As String, Optional IsFile As Boolean) As String

参数:

参数类型说明
PathString目录路径或文件路径
IsFileBoolean可选。如果为 True,则自动提取父目录创建

返回值:

  • 创建的目录路径(已清理格式)

说明:

  • 使用 Windows API MakeSureDirectoryPathExists 创建目录
  • 支持多级目录创建
  • 目录已存在时不会报错

示例:

vb
Dim Fso As New cToolsFso

' 创建目录
Dim DirPath As String
DirPath = Fso.AutoMakeDir("C:\\MyApp\\Data\\Logs", False)
Debug.Print "已创建目录: " & DirPath

' 从文件路径中提取并创建目录
DirPath = Fso.AutoMakeDir("C:\\MyApp\\Data\\file.txt", True)
' 会创建 C:\MyApp\Data 目录

AppPath

获取应用程序路径(根据运行模式自动调整)。

语法:

vb
Public Function AppPath(Optional ByVal Path As String) As String

参数:

参数类型说明
PathString可选。要追加的子路径

返回值:

  • 应用程序路径字符串

说明:

  • IDE 模式(App.LogMode = 0):返回 App.Path\..\dist\EXE\
  • 编译模式:返回 App.Path\

示例:

vb
Dim Fso As New cToolsFso

' 获取应用路径
Debug.Print Fso.AppPath()

' 获取应用路径下的子目录
Debug.Print Fso.AppPath("data\config.ini")

MakeNewFileFulPath

生成新的文件名(添加后缀)。

语法:

vb
Public Function MakeNewFileFulPath(FileSrc As String, AppendFix As String, Optional JoinStr As String = "_") As String

参数:

参数类型说明
FileSrcString原始文件路径
AppendFixString要添加的后缀
JoinStrString可选。连接符,默认为 _

返回值:

  • 新的文件名(不含路径)

说明:

  • 保留原文件扩展名
  • 在文件名和扩展名之间插入连接符和后缀

示例:

vb
Dim Fso As New cToolsFso

' 生成备份文件名
Debug.Print Fso.MakeNewFileFulPath("C:\\data\\document.txt", "backup")
' 输出: document_backup.txt

' 使用自定义连接符
Debug.Print Fso.MakeNewFileFulPath("photo.jpg", "2024", "-")
' 输出: photo-2024.jpg

使用示例

综合示例:文件备份

vb
Sub BackupFileExample()
    Dim Fso As New cToolsFso
    Dim SrcFile As String
    Dim BackupDir As String
    Dim BackupFile As String
    
    ' 源文件
    SrcFile = "C:\\MyApp\\data\\important.dat"
    
    ' 确保备份目录存在
    BackupDir = Fso.AutoMakeDir("C:\\MyApp\\backup\\", False)
    
    ' 生成备份文件名
    BackupFile = BackupDir & Fso.MakeNewFileFulPath(SrcFile, Format(Now, "yyyymmdd"))
    
    ' 使用 FSO 复制文件
    Fso.Inst.CopyFile SrcFile, BackupFile, True
    
    Debug.Print "备份完成: " & BackupFile
End Sub

综合示例:遍历目录

vb
Sub ListDirectoryExample()
    Dim Fso As New cToolsFso
    Dim Folder As Scripting.Folder
    Dim SubFolder As Scripting.Folder
    Dim File As Scripting.File
    
    ' 获取目录对象
    Set Folder = Fso.Inst.GetFolder("C:\\Windows")
    
    ' 遍历子目录
    Debug.Print "=== 子目录 ==="
    For Each SubFolder In Folder.SubFolders
        Debug.Print SubFolder.Name & " (" & SubFolder.Size & " bytes)"
    Next
    
    ' 遍历文件
    Debug.Print "=== 文件 ==="
    For Each File In Folder.Files
        Debug.Print File.Name & " (" & File.Size & " bytes)"
    Next
End Sub

VB6及其LOGO版权为微软公司所有