VBMAN.ToolsFso - File System Operation Object
Overview
VBMAN.ToolsFso provides convenient file system operations, encapsulating common file and directory processing functions, simplifying file system programming.
Core Features
- Auto Directory Creation: Create multi-level directories at once
- Path Processing: Auto-complete, clean path separators
- File Info: Get file name, extension, directory name, etc.
- Reference Encapsulation: Direct access to FileSystemObject instance
Properties
| Property | Type | Description |
|---|---|---|
Inst | Scripting.FileSystemObject | FSO instance object |
Methods
AutoMakeDir
Auto-create directory (supports multi-level)
vb
Public Function AutoMakeDir(ByVal Path As String, Optional IsFile As Boolean) As StringParameters:
Path- Directory path or file pathIsFile- Whether it's a file path (True extracts directory part to create)
Returns: Cleaned directory path
Example:
vb
' Create multi-level directory
VBMAN.ToolsFso.AutoMakeDir "C:\\MyApp\\Data\\Users"
' Auto-extract directory to create (for file paths)
VBMAN.ToolsFso.AutoMakeDir "C:\\MyApp\\Logs\\app.log", True
' Actually creates C:\MyApp\Logs directory
' Return value can be used for confirmation
Dim dirPath As String
dirPath = VBMAN.ToolsFso.AutoMakeDir("C:\\MyApp\\Data")
Debug.Print "Created/confirmed directory: " & dirPathClearSpan
Clean path separators (unify to backslash)
vb
Public Function ClearSpan(Path As String, Optional IsFile As Boolean) As StringExample:
vb
' Unify path separators
Dim path1 As String
path1 = VBMAN.ToolsFso.ClearSpan("C:/MyApp//Data\\Users/")
' Result: C:\MyApp\Data\Users\
' File path (no trailing separator added)
Dim path2 As String
path2 = VBMAN.ToolsFso.ClearSpan("C:/MyApp//file.txt", True)
' Result: C:\MyApp\file.txtIsFullPath
Check if it's a full path
vb
Public Function IsFullPath(Path As String) As BooleanExample:
vb
If VBMAN.ToolsFso.IsFullPath("C:\\test.txt") Then
Debug.Print "Full path"
Else
Debug.Print "Relative path"
End IfAutoCompleteFullPath
Auto-complete to full path
vb
Public Function AutoCompleteFullPath(Path As String, Optional IsFile As Boolean) As StringExample:
vb
' Relative path to full path
Dim fullPath As String
fullPath = VBMAN.ToolsFso.AutoCompleteFullPath("data\\config.ini")
' Result: C:\Program\MyApp\data\config.ini (based on App.Path)AppPath
Get application path (supports development/runtime mode switching)
vb
Public Function AppPath(Optional ByVal Path As String) As StringDescription: Development mode (in IDE) returns App.Path\..\dist\EXE\, runtime mode returns App.Path\
Example:
vb
' Get application-related paths
Dim configPath As String
configPath = VBMAN.ToolsFso.AppPath("config.ini")
' Development mode: C:\Project\..\dist\EXE\config.ini
' Runtime mode: C:\Program\MyApp\config.iniMakeNewFileFulPath
Generate new file path (add suffix)
vb
Public Function MakeNewFileFulPath(FileSrc As String, AppendFix As String, Optional JoinStr As String = "_") As StringExample:
vb
' Generate backup file name
Dim backupFile As String
backupFile = VBMAN.ToolsFso.MakeNewFileFulPath("C:\\data\\report.pdf", "backup")
' Result: report_backup.pdf
' Use timestamp
Dim datedFile As String
datedFile = VBMAN.ToolsFso.MakeNewFileFulPath("report.pdf", Format(Now, "yyyymmdd"))
' Result: report_20240115.pdfInst Property Usage
ToolsFso.Inst is an instance of Scripting.FileSystemObject, which can use all FSO methods:
vb
' Check if file exists
If VBMAN.ToolsFso.Inst.FileExists("C:\\test.txt") Then
Debug.Print "File exists"
End If
' Check if directory exists
If VBMAN.ToolsFso.Inst.FolderExists("C:\\MyApp") Then
Debug.Print "Directory exists"
End If
' Get file name (without extension)
Dim baseName As String
baseName = VBMAN.ToolsFso.Inst.GetBaseName("C:\\data\\report.pdf")
' Result: report
' Get extension
Dim ext As String
ext = VBMAN.ToolsFso.Inst.GetExtensionName("report.pdf")
' Result: pdf
' Get directory name
Dim folder As String
folder = VBMAN.ToolsFso.Inst.GetParentFolderName("C:\\data\\file.txt")
' Result: C:\data
' Get full file name
Dim fileName As String
fileName = VBMAN.ToolsFso.Inst.GetFileName("C:\\data\\file.txt")
' Result: file.txt
' Copy file
VBMAN.ToolsFso.Inst.CopyFile "source.txt", "dest.txt", True
' Move file
VBMAN.ToolsFso.Inst.MoveFile "old.txt", "new.txt"
' Delete file
VBMAN.ToolsFso.Inst.DeleteFile "temp.txt", True
' Copy directory
VBMAN.ToolsFso.Inst.CopyFolder "C:\\source", "C:\\dest", True
' Get file object
Dim fileObj As File
Set fileObj = VBMAN.ToolsFso.Inst.GetFile("C:\\test.txt")
Debug.Print fileObj.Size ' File size
Debug.Print fileObj.DateCreated ' Creation time
' Get directory object
Dim folderObj As Folder
Set folderObj = VBMAN.ToolsFso.Inst.GetFolder("C:\\MyApp")
Debug.Print folderObj.SubFolders.Count ' Subdirectory count
Debug.Print folderObj.Files.Count ' File countComprehensive Examples
Example 1: Log Directory Initialization
vb
Private Sub InitLogDirectory()
' Ensure log directory exists
Dim logDir As String
logDir = VBMAN.ToolsFso.AppPath("logs")
VBMAN.ToolsFso.AutoMakeDir logDir
' Create date-based subdirectory
Dim todayDir As String
todayDir = logDir & "\\" & Format(Now, "yyyy-MM-dd")
VBMAN.ToolsFso.AutoMakeDir todayDir
Debug.Print "Log directory: " & todayDir
End SubExample 2: Safe File Save
vb
Private Sub SaveFileSafely(sourcePath As String)
' Check if source file exists
If Not VBMAN.ToolsFso.Inst.FileExists(sourcePath) Then
MsgBox "Source file does not exist!"
Exit Sub
End If
' Ensure destination directory exists
Dim destDir As String
destDir = App.Path & "\\Backup"
VBMAN.ToolsFso.AutoMakeDir destDir
' Generate backup file name
Dim destPath As String
Dim fileName As String
fileName = VBMAN.ToolsFso.Inst.GetFileName(sourcePath)
destPath = destDir & "\\" & VBMAN.ToolsFso.MakeNewFileFulPath(fileName, Format(Now, "yyyymmdd_hhmmss"))
' Copy file
VBMAN.ToolsFso.Inst.CopyFile sourcePath, destPath, True
MsgBox "File backed up to: " & destPath
End SubExample 3: Traverse Directory Files
vb
Private Sub ListAllFiles(folderPath As String, listBox As ListBox)
If Not VBMAN.ToolsFso.Inst.FolderExists(folderPath) Then
MsgBox "Directory does not exist!"
Exit Sub
End If
Dim folder As Folder
Dim file As File
Dim subFolder As Folder
Set folder = VBMAN.ToolsFso.Inst.GetFolder(folderPath)
' Add current directory files
For Each file In folder.Files
listBox.AddItem file.Path & " (" & file.Size & " bytes)"
Next file
' Recursively traverse subdirectories
For Each subFolder In folder.SubFolders
ListAllFiles subFolder.Path, listBox
Next subFolder
End SubExample 4: Clean Temporary Files
vb
Private Sub CleanTempFiles()
Dim tempDir As String
tempDir = App.Path & "\\Temp"
If Not VBMAN.ToolsFso.Inst.FolderExists(tempDir) Then
Exit Sub
End If
Dim folder As Folder
Dim file As File
Dim deletedCount As Long
Set folder = VBMAN.ToolsFso.Inst.GetFolder(tempDir)
For Each file In folder.Files
' Delete files older than 7 days
If DateDiff("d", file.DateLastModified, Now) > 7 Then
file.Delete True
deletedCount = deletedCount + 1
End If
Next file
MsgBox "Cleaned " & deletedCount & " temporary files"
End SubBest Practices
- Use AutoMakeDir: Ensure directory exists before file operations
- Use AppPath: Handle path differences between development and runtime environments
- Check Existence: Check if file/directory exists before operations
- Error Handling: Add On Error handling for file operation exceptions
- Release in Time: Set File/Folder objects to Nothing after use