Skip to content

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 NameTypeDescription
InstScripting.FileSystemObjectFSO instance object, can directly access original FSO methods

Methods

IsFullPath

Determines whether the path is a full path (contains drive letter).

Syntax:

vb
Public Function IsFullPath(Path As String) As Boolean

Parameters:

ParameterTypeDescription
PathStringPath to check

Returns:

  • True - Is full path (contains :\)
  • False - Is relative path

Example:

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

Auto-completes to full path, supports relative path to absolute path conversion.

Syntax:

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

Parameters:

ParameterTypeDescription
PathStringOriginal path
IsFileBooleanOptional. 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:

vb
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.txt

ClearSpan

Cleans up slashes in the path, unified to backslash format.

Syntax:

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

Parameters:

ParameterTypeDescription
PathStringPath to clean
IsFileBooleanOptional. 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:

vb
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:

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

Parameters:

ParameterTypeDescription
PathStringDirectory path or file path
IsFileBooleanOptional. If True, automatically extracts parent directory to create

Returns:

  • Created directory path (cleaned format)

Description:

  • Uses Windows API MakeSureDirectoryPathExists to create directory
  • Supports multi-level directory creation
  • Does not report error if directory already exists

Example:

vb
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 directory

AppPath

Gets the application path (automatically adjusts based on run mode).

Syntax:

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

Parameters:

ParameterTypeDescription
PathStringOptional. Sub-path to append

Returns:

  • Application path string

Description:

  • IDE mode (App.LogMode = 0): returns App.Path\..\dist\EXE\
  • Compiled mode: returns App.Path\

Example:

vb
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:

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

Parameters:

ParameterTypeDescription
FileSrcStringOriginal file path
AppendFixStringSuffix to add
JoinStrStringOptional. 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:

vb
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.jpg

Usage Examples

Comprehensive Example: File Backup

vb
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 Sub

Comprehensive Example: Traverse Directory

vb
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

VB6 and LOGO copyright of Microsoft Corporation