cToolsFso Class
File system operation utility class, providing common functions like path processing and directory management.
Description
cToolsFso is a wrapper for Scripting.FileSystemObject, providing more convenient file path processing and directory operation functionality. The original FSO object is exposed through the Inst property for advanced operations.
Required Library:
Microsoft Scripting Runtime(scrrun.dll)
Properties
| Property Name | Type | Description |
|---|---|---|
Inst | Scripting.FileSystemObject | FSO instance object, can directly access original FSO methods |
Methods
IsFullPath
Determines whether the path is a full path (contains drive letter).
Syntax:
Public Function IsFullPath(Path As String) As BooleanParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Path to check |
Returns:
True- Is full path (contains:\)False- Is relative path
Example:
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") ' FalseAutoCompleteFullPath
Auto-completes to full path, supports relative path to absolute path conversion.
Syntax:
Public Function AutoCompleteFullPath(Path As String, Optional IsFile As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Original path |
IsFile | Boolean | Optional. Whether it's a file path (affects trailing slash handling) |
Returns:
- Full path string
Description:
- If path is already a full path, returns it directly
- If relative path, prepends
App.Path - Automatically cleans up extra slashes in path
Example:
Dim Fso As New cToolsFso
' Relative path to absolute path
Debug.Print Fso.AutoCompleteFullPath("data\config.ini")
' Output: C:\Program Files\MyApp\data\config.ini
' Already full path, remains unchanged
Debug.Print Fso.AutoCompleteFullPath("D:\\backup\\data.txt")
' Output: D:\backup\data.txtClearSpan
Cleans up slashes in the path, unified to backslash format.
Syntax:
Public Function ClearSpan(Path As String, Optional IsFile As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Path to clean |
IsFile | Boolean | Optional. Whether it's a file path (directory will auto-add trailing slash) |
Returns:
- Cleaned path string
Description:
- Unifies
//,\/,/to\ - If directory path, automatically adds trailing
\
Example:
Dim Fso As New cToolsFso
' Unify slash format
Debug.Print Fso.ClearSpan("C://Windows\\System32//file.txt", True)
' Output: C:\Windows\System32\file.txt
' Directory path auto-adds trailing slash
Debug.Print Fso.ClearSpan("C:\\Windows\\System32", False)
' Output: C:\Windows\System32\AutoMakeDir
Automatically creates a directory (if it doesn't exist).
Syntax:
Public Function AutoMakeDir(ByVal Path As String, Optional IsFile As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Directory path or file path |
IsFile | Boolean | Optional. If True, automatically extracts parent directory to create |
Returns:
- Created directory path (cleaned format)
Description:
- Uses Windows API
MakeSureDirectoryPathExiststo create directory - Supports multi-level directory creation
- Does not report error if directory already exists
Example:
Dim Fso As New cToolsFso
' Create directory
Dim DirPath As String
DirPath = Fso.AutoMakeDir("C:\\MyApp\\Data\\Logs", False)
Debug.Print "Directory created: " & DirPath
' Extract and create directory from file path
DirPath = Fso.AutoMakeDir("C:\\MyApp\\Data\\file.txt", True)
' Creates C:\MyApp\Data directoryAppPath
Gets the application path (automatically adjusts based on run mode).
Syntax:
Public Function AppPath(Optional ByVal Path As String) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Optional. Sub-path to append |
Returns:
- Application path string
Description:
- IDE mode (
App.LogMode = 0): returnsApp.Path\..\dist\EXE\ - Compiled mode: returns
App.Path\
Example:
Dim Fso As New cToolsFso
' Get application path
Debug.Print Fso.AppPath()
' Get subdirectory under application path
Debug.Print Fso.AppPath("data\config.ini")MakeNewFileFulPath
Generates a new file name (adds suffix).
Syntax:
Public Function MakeNewFileFulPath(FileSrc As String, AppendFix As String, Optional JoinStr As String = "_") As StringParameters:
| Parameter | Type | Description |
|---|---|---|
FileSrc | String | Original file path |
AppendFix | String | Suffix to add |
JoinStr | String | Optional. Join string, default is _ |
Returns:
- New file name (without path)
Description:
- Preserves original file extension
- Inserts join string and suffix between filename and extension
Example:
Dim Fso As New cToolsFso
' Generate backup filename
Debug.Print Fso.MakeNewFileFulPath("C:\\data\\document.txt", "backup")
' Output: document_backup.txt
' Use custom join string
Debug.Print Fso.MakeNewFileFulPath("photo.jpg", "2024", "-")
' Output: photo-2024.jpgUsage Examples
Comprehensive Example: File Backup
Sub BackupFileExample()
Dim Fso As New cToolsFso
Dim SrcFile As String
Dim BackupDir As String
Dim BackupFile As String
' Source file
SrcFile = "C:\\MyApp\\data\\important.dat"
' Ensure backup directory exists
BackupDir = Fso.AutoMakeDir("C:\\MyApp\\backup\\", False)
' Generate backup filename
BackupFile = BackupDir & Fso.MakeNewFileFulPath(SrcFile, Format(Now, "yyyymmdd"))
' Copy file using FSO
Fso.Inst.CopyFile SrcFile, BackupFile, True
Debug.Print "Backup completed: " & BackupFile
End SubComprehensive Example: Traverse Directory
Sub ListDirectoryExample()
Dim Fso As New cToolsFso
Dim Folder As Scripting.Folder
Dim SubFolder As Scripting.Folder
Dim File As Scripting.File
' Get directory object
Set Folder = Fso.Inst.GetFolder("C:\\Windows")
' Traverse subdirectories
Debug.Print "=== Subdirectories ==="
For Each SubFolder In Folder.SubFolders
Debug.Print SubFolder.Name & " (" & SubFolder.Size & " bytes)"
Next
' Traverse files
Debug.Print "=== Files ==="
For Each File In Folder.Files
Debug.Print File.Name & " (" & File.Size & " bytes)"
Next
End Sub