Tools - File System Utilities
Detailed Documentation: Each class has its own detailed documentation with complete member descriptions and example code:
- cToolsFso.md - File System Tools Class
- cFileIO.md - API File Operations Class
- cFileEx.md - VB Native File Operations Class
- cToolsStream.md - ADO Stream File Operations Class
- ToolsFso.md - FSO Module
cToolsFso - File System Tools
Overview
Provides file path processing, auto-completion, directory creation, and other functionality.
Properties
Inst
Returns a Scripting.FileSystemObject instance.
Public Inst As New Scripting.FileSystemObjectExample:
' Use Inst to access FSO functionality
Dim Folder As Scripting.Folder
Set Folder = VBMAN.ToolsFso.Inst.GetFolder("C:\\MyFolder")
Dim File As Scripting.File
Set File = VBMAN.ToolsFso.Inst.GetFile("C:\\file.txt")Path Processing
IsFullPath
Checks if a path is a full path (contains drive letter).
Public Function IsFullPath(Path As String) As BooleanReturns:
True- Is relative path (does not contain:\)False- Is absolute path
Example:
Debug.Print VBMAN.ToolsFso.IsFullPath("C:\\test.txt") ' False (is full path)
Debug.Print VBMAN.ToolsFso.IsFullPath("test.txt") ' True (is relative path)AutoCompleteFullPath
Auto-completes to full path (relative path to absolute path).
Public Function AutoCompleteFullPath(Path As String, Optional IsFile As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Source path |
IsFile | Boolean | Whether it's a file path |
Example:
Dim FullPath As String
FullPath = VBMAN.ToolsFso.AutoCompleteFullPath("data\\config.ini")
' Result: C:\\MyApp\\data\\config.iniClearSpan
Cleans up extra slashes in the path, unified to backslash.
Public Function ClearSpan(Path As String, Optional IsFile As Boolean) As StringExample:
Debug.Print VBMAN.ToolsFso.ClearSpan("C:\\\\temp\\\\file.txt")
' Output: C:\\temp\\file.txt
Debug.Print VBMAN.ToolsFso.ClearSpan("C://temp//folder//", False)
' Output: C:\\temp\\folder\\Directory Operations
AutoMakeDir
Automatically creates a directory (if it doesn't exist).
Public Function AutoMakeDir(ByVal Path As String, Optional IsFile As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Path | String | Path |
IsFile | Boolean | Whether it's a file path (if True, extracts directory part) |
Returns:
The created directory path.
Example:
' Create directory
VBMAN.ToolsFso.AutoMakeDir "C:\\MyApp\\Logs"
' If file path, automatically extracts directory part and creates
VBMAN.ToolsFso.AutoMakeDir "C:\\MyApp\\Logs\\app.log", True
' Creates C:\\MyApp\\Logs directoryApplication Path
AppPath
Gets the application path (automatically handles IDE and compiled path differences).
Public Function AppPath(Optional ByVal Path As String) As StringDescription:
- IDE environment returns:
App.Path & "\..\dist\EXE\" - Compiled returns:
App.Path & "\"
Example:
' IDE environment: C:\\MyProject\\..\\dist\\EXE\\
' Compiled: C:\\Program Files\\MyApp\\
Dim ConfigPath As String
ConfigPath = VBMAN.ToolsFso.AppPath("config.ini")
Debug.Print ConfigPathFile Name Processing
MakeNewFileFulPath
Generates a new file full path (adds suffix).
Public Function MakeNewFileFulPath(FileSrc As String, AppendFix As String, Optional JoinStr As String = "_") As StringParameters:
| Parameter | Type | Description |
|---|---|---|
FileSrc | String | Source file path |
AppendFix | String | Suffix to add |
JoinStr | String | Join string (default "_") |
Example:
Dim NewPath As String
NewPath = VBMAN.ToolsFso.MakeNewFileFulPath("C:\\file.txt", "backup")
' Output: file_backup.txt
NewPath = VBMAN.ToolsFso.MakeNewFileFulPath("C:\\data\\report.pdf", "2024", "-")
' Output: report-2024.pdfcFileIO - Advanced File Operations (API Implementation)
Overview
Advanced file operations using Windows CreateFile API, supports large files (>2GB).
Properties
| Property | Description |
|---|---|
IsOpen | Whether file is open (Boolean, read-only) |
FilePath | Current file path (String, read-only) |
LastError | Last error message (String, read-only) |
FileSize | File size (Currency, in bytes, read-only) |
BufferSize | Current buffer byte count (Long, read-only) |
File Open/Close
OpenFile
Opens a file, returns Me to support method chaining.
Public Function OpenFile(ByVal FilePath As String, Optional ByVal AccessMode As String = "RW") As cFileIOParameters:
| Parameter | Type | Description |
|---|---|---|
FilePath | String | File path |
AccessMode | String | Access mode: "R"=read-only, "W"=write-only, "RW"=read-write (default) |
Example:
' Open in read-only mode
VBMAN.FileIO.OpenFile "C:\\data.txt", "R"
' Open in read-write mode
VBMAN.FileIO.OpenFile "C:\\data.txt", "RW"
' Method chaining
Dim Content As String
Content = VBMAN.FileIO.OpenFile("C:\\data.txt").ReadData().ReturnText()CloseFile
Closes the file.
Public Sub CloseFile()Example:
VBMAN.FileIO.CloseFileBuffer Operations
ClearBuffer
Clears the internal buffer.
Public Function ClearBuffer() As cFileIOSetBuffer
Fills the buffer with external byte array.
Public Function SetBuffer(Data() As Byte) As cFileIOExample:
Dim Bytes() As Byte
Bytes = StrConv("Hello", vbFromUnicode)
VBMAN.FileIO.SetBuffer BytesAppendBuffer
Appends bytes to the buffer.
Public Function AppendBuffer(Data() As Byte) As cFileIOExample:
Dim Bytes1() As Byte, Bytes2() As Byte
Bytes1 = StrConv("Hello", vbFromUnicode)
Bytes2 = StrConv(" World", vbFromUnicode)
VBMAN.FileIO.SetBuffer(Bytes1).AppendBuffer Bytes2SetBufferText
Fills the buffer with string (default UTF-8).
Public Function SetBufferText(ByVal Text As String, Optional ByVal CharSet As String = "UTF-8") As cFileIOExample:
VBMAN.FileIO.SetBufferText "Hello World", "UTF-8"Data Reading
ReadData
Reads specified range of file into internal buffer.
Public Function ReadData(Optional ByVal StartPos As Long = -1, Optional ByVal EndPos As Long = -1) As cFileIOParameters:
| Parameter | Type | Description |
|---|---|---|
StartPos | Long | Start position (0-based), -1 means from beginning |
EndPos | Long | End position (0-based), -1 means to end |
Example:
' Read entire file
VBMAN.FileIO.OpenFile("C:\\data.txt").ReadData
' Read specified range
VBMAN.FileIO.OpenFile("C:\\data.txt").ReadData 0, 1023 ' Read first 1KB
' Method chaining to read and get text
Dim Text As String
Text = VBMAN.FileIO.OpenFile("C:\\data.txt").ReadData().ReturnText()Data Return
ReturnText
Returns buffer content as text.
Public Function ReturnText(Optional ByVal CharSet As String = "UTF-8") As StringExample:
Dim Text As String
VBMAN.FileIO.OpenFile("C:\\utf8.txt").ReadData()
Text = VBMAN.FileIO.ReturnText("UTF-8")ReturnBytes
Returns byte array (copy).
Public Function ReturnBytes() As Byte()Example:
Dim Bytes() As Byte
VBMAN.FileIO.OpenFile("C:\\data.bin").ReadData()
Bytes = VBMAN.FileIO.ReturnBytes()ReturnBase64
Returns Base64-encoded string.
Public Function ReturnBase64() As StringExample:
Dim Base64 As String
VBMAN.FileIO.OpenFile("C:\\image.png").ReadData()
Base64 = VBMAN.FileIO.ReturnBase64()Data Writing
SaveData
Writes internal buffer to file.
Public Function SaveData(Optional ByVal FilePath As String, Optional ByVal IsAppend As Boolean = False) As cFileIOParameters:
| Parameter | Type | Description |
|---|---|---|
FilePath | String | Target path, uses currently opened file if empty |
IsAppend | Boolean | False=overwrite (default), True=append |
Example:
' Overwrite
VBMAN.FileIO.SetBufferText("Hello World").SaveData "C:\\output.txt"
' Append
VBMAN.FileIO.SetBufferText("New Line").SaveData "C:\\log.txt", True
' Write to currently opened file
VBMAN.FileIO.OpenFile("C:\\data.txt", "RW").SetBufferText("Content").SaveDataComplete Example
Private Sub FileIODemo()
' Read text file
Dim Content As String
Content = VBMAN.FileIO.OpenFile("C:\\input.txt").ReadData().ReturnText()
Debug.Print Content
' Read and convert
Dim Base64 As String
Base64 = VBMAN.FileIO.OpenFile("C:\\image.png").ReadData().ReturnBase64()
' Write file
VBMAN.FileIO.SetBufferText("Hello World").SaveData "C:\\output.txt"
' Append content
VBMAN.FileIO.SetBufferText("Line 1" & vbCrLf).SaveData "C:\\log.txt", True
VBMAN.FileIO.SetBufferText("Line 2" & vbCrLf).SaveData "C:\\log.txt", True
' Copy file (chunked reading for large files)
With VBMAN.FileIO
.OpenFile "C:\\largefile.zip"
Dim FileLen As Currency
FileLen = .FileSize
' Read in 2MB chunks
Dim Pos As Long
For Pos = 0 To FileLen Step 2097152
.ReadData Pos, Pos + 2097151
.SaveData "C:\\copy.zip", True
Next Pos
End With
End SubcFileEx - Advanced File Operations (Open Statement Implementation)
Overview
File operations using VB native Open statement, with good compatibility, interface consistent with cFileIO.
Difference from cFileIO
| Feature | cFileIO | cFileEx |
|---|---|---|
| Implementation | CreateFile API | VB Open statement |
| Large file support | >2GB | Limited by VB |
| Compatibility | Requires Windows API | Native VB, better compatibility |
| Performance | High | Moderate |
Properties
Same as cFileIO:
IsOpen- Whether file is openFilePath- Current file pathLastError- Last error messageFileSize- File sizeBufferSize- Current buffer byte count
Methods
Same as cFileIO, with additional support:
SetBufferBase64
Fills the buffer with Base64 string.
Public Function SetBufferBase64(ByVal Base64String As String) As cFileExExample:
VBMAN.FileEx.SetBufferBase64 "SGVsbG8gV29ybGQ="SetBufferHex
Fills the buffer with Hex string.
Public Function SetBufferHex(ByVal HexString As String, Optional ByVal Separator As String = " ") As cFileExExample:
VBMAN.FileEx.SetBufferHex "48 65 6C 6C 6F"
VBMAN.FileEx.SetBufferHex "48656C6C6F", "" ' Without separatorSetBufferBinString
Fills the buffer with binary string.
Public Function SetBufferBinString(ByVal BinString As String, Optional ByVal Separator As String = " ") As cFileExExample:
VBMAN.FileEx.SetBufferBinString "01001000 01100101 01101100 01101100 01101111"ReturnHex
Returns Hex-encoded string.
Public Function ReturnHex(Optional ByVal Separator As String = " ") As StringExample:
VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData()
Debug.Print VBMAN.FileEx.ReturnHex(" ") ' Output: 48 65 6C 6C 6F
Debug.Print VBMAN.FileEx.ReturnHex("") ' Output: 48656C6C6FReturnBinString
Returns binary string.
Public Function ReturnBinString(Optional ByVal Separator As String = " ") As StringExample:
Debug.Print VBMAN.FileEx.ReturnBinString(" ") ' Output: 01001000 01100101...cToolsStream - Stream File Operations
Overview
File stream operations based on ADODB.Stream, supporting line-by-line reading/writing and multiple encodings.
Properties
| Property | Type | Description |
|---|---|---|
Inst | ADODB.Stream | Stream instance |
LastError | String | Last error message |
UseLine | Boolean | Whether line-by-line mode is enabled |
LineData | cCollection | Line data collection |
LineNumber | Long | Current line number |
Methods
UseLineMode
Sets whether to enable line-by-line read/write mode (method chaining).
Public Function UseLineMode(v As Boolean) As cToolsStreamExample:
VBMAN.ToolsStream.UseLineMode(True)LoadFileAsText
Loads a text file.
Public Function LoadFileAsText(ByVal FileName As String, Optional ByVal CharSet As String = "UTF-8") As StringExample:
Dim Content As String
Content = VBMAN.ToolsStream.LoadFileAsText("test.txt", "UTF-8")SaveFileAsText
Saves a text file.
Public Function SaveFileAsText(ByVal FileName As String, Optional Data As Variant, Optional ByVal CharSet As String = "UTF-8") As BooleanExample:
' Save data directly
VBMAN.ToolsStream.SaveFileAsText "output.txt", "Hello World", "UTF-8"
' Use line mode to save
VBMAN.ToolsStream.UseLineMode(True)
VBMAN.ToolsStream.WriteLine "Line 1"
VBMAN.ToolsStream.WriteLine "Line 2"
VBMAN.ToolsStream.SaveFileAsText "output.txt", , "UTF-8"LoadFileAsBinary
Loads file as binary.
Public Function LoadFileAsBinary(ByVal Path As String, OutData() As Byte) As BooleanExample:
Dim Data() As Byte
VBMAN.ToolsStream.LoadFileAsBinary "image.png", DataSaveFileAsBinary
Saves file as binary.
Public Function SaveFileAsBinary(ByVal Path As String, OutData() As Byte) As BooleanExample:
Dim Data() As Byte
' ... fill Data
VBMAN.ToolsStream.SaveFileAsBinary "output.bin", DataReadLine
Reads a line (requires line mode enabled).
Public Function ReadLine(Optional ByVal Line As Long) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Line | Long | Specify line number, 0 means current line |
Example:
' Read file line by line
With VBMAN.ToolsStream.UseLineMode(True)
.LoadFileAsText "log.txt", "UTF-8"
Dim Line As String
Line = .ReadLine(1) ' Read line 1
Debug.Print Line
Line = .ReadLine() ' Read next line (auto increment)
Debug.Print Line
End WithWriteLine
Writes a line (requires line mode enabled).
Public Function WriteLine(Text As String, Optional ByVal Line As Long) As BooleanExample:
' Write file line by line
With VBMAN.ToolsStream.UseLineMode(True)
.WriteLine "First line content"
.WriteLine "Second line content"
.WriteLine "Third line content", 10 ' Write to line 10
.SaveFileAsText "output.txt", , "UTF-8"
End WithComplete Example
Private Sub FileOperationsDemo()
' ========== cToolsFso Examples ==========
' Create directory
VBMAN.ToolsFso.AutoMakeDir "C:\\MyApp\\Logs"
' Get application path
Dim AppPath As String
AppPath = VBMAN.ToolsFso.AppPath("config.ini")
' Generate backup file name
Dim BackupName As String
BackupName = VBMAN.ToolsFso.MakeNewFileFulPath("report.pdf", "backup")
' ========== cFileIO Examples ==========
' Read large file
Dim LargeFileContent As String
With VBMAN.FileIO
.OpenFile "C:\\largefile.bin"
LargeFileContent = .ReadData().ReturnBase64()
End With
' Write file
VBMAN.FileIO.SetBufferText("Hello World").SaveData "C:\\test.txt"
' ========== cFileEx Examples ==========
' Hex operations
VBMAN.FileEx.SetBufferHex "48 65 6C 6C 6F"
VBMAN.FileEx.SaveData "C:\\hex_test.bin"
' ========== cToolsStream Examples ==========
' Process config file line by line
With VBMAN.ToolsStream.UseLineMode(True)
.LoadFileAsText "config.ini", "UTF-8"
Dim i As Long
For i = 1 To 10
Debug.Print "Line " & i & ": " & .ReadLine(i)
Next i
End With
End Sub