VBMAN.FileEx - Advanced File Operation Object
Overview
VBMAN.FileEx provides advanced file operation functionality, implemented based on VB's native Open statement, supporting multiple encoding formats, flexible data read/write methods, and buffer operations.
Core Features
- Multi-encoding Support: UTF-8, ANSI, UTF-16LE auto-detection
- Flexible Modes: Read-only, write-only, read-write modes
- Buffer Operations: Supports byte arrays, Base64, Hex, and other formats
- Chain Calling: Fluent API design
- Large File Support: Supports file operations up to 2GB
Properties
| Property | Type | Description |
|---|---|---|
IsOpen | Boolean | Whether file is open |
FilePath | String | Currently open file path |
LastError | String | Last error message |
FileSize | Currency | File size (bytes) |
BufferSize | Long | Current buffer byte count |
Methods
File Open/Close
OpenFile
Open file
Public Function OpenFile(ByVal FilePath As String, Optional ByVal AccessMode As String = "RW") As cFileExParameters:
FilePath- File pathAccessMode- Access mode: "R"=Read-only, "W"=Write-only, "RW"=Read-write (default)
Example:
' Read-only mode
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
' Read-write mode (default)
VBMAN.FileEx.OpenFile "C:\\data.txt"
' Chain calling
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnTextCloseFile
Close file
Public Sub CloseFile()Example:
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
' ... operations ...
VBMAN.FileEx.CloseFileBuffer Settings
ClearBuffer
Clear internal buffer
Public Function ClearBuffer() As cFileExSetBuffer
Fill buffer with byte array
Public Function SetBuffer(Data() As Byte) As cFileExExample:
Dim bytes(0 To 3) As Byte
bytes(0) = &H41: bytes(1) = &H42
bytes(2) = &H43: bytes(3) = &H44
VBMAN.FileEx.SetBuffer bytesSetBufferText
Fill buffer with string (specify encoding)
Public Function SetBufferText(ByVal Text As String, Optional ByVal CharSet As String = "UTF-8") As cFileExExample:
VBMAN.FileEx.SetBufferText "Hello World", "UTF-8"SetBufferBase64
Fill buffer with Base64 string
Public Function SetBufferBase64(ByVal Base64String As String) As cFileExExample:
VBMAN.FileEx.SetBufferBase64 "SGVsbG8gV29ybGQ="SetBufferHex
Fill 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"SetBufferBinString
Fill buffer with binary string
Public Function SetBufferBinString(ByVal BinString As String, Optional ByVal Separator As String = " ") As cFileExExample:
VBMAN.FileEx.SetBufferBinString "01001000 01100101"AppendBuffer
Append bytes to buffer
Public Function AppendBuffer(Data() As Byte) As cFileExData Reading
ReadData
Read file data to internal buffer
Public Function ReadData(Optional ByVal StartPos As Long = -1, Optional ByVal EndPos As Long = -1) As cFileExParameters:
StartPos- Start position (1-based, -1=file beginning)EndPos- End position (1-based, -1=file end)
Example:
' Read entire file
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnText
' Read specified range
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
VBMAN.FileEx.ReadData 1, 100 ' Read first 100 bytesData Return
ReturnText
Return buffer content as text
Public Function ReturnText(Optional ByVal CharSet As String = "UTF-8") As StringExample:
Dim text As String
text = VBMAN.FileEx.OpenFile("C:\\utf8.txt").ReadData.ReturnText("UTF-8")ReturnBytes
Return buffer content as byte array
Public Function ReturnBytes() As Byte()Example:
Dim bytes() As Byte
bytes = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBytesReturnBase64
Return buffer content as Base64 string
Public Function ReturnBase64() As StringExample:
Dim base64 As String
base64 = VBMAN.FileEx.OpenFile("C:\\image.png").ReadData.ReturnBase64ReturnHex
Return buffer content as Hex string
Public Function ReturnHex(Optional ByVal Separator As String = " ") As StringExample:
Dim hex As String
hex = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnHex(" ")
' Result: 48 65 6C 6C 6FReturnBinString
Return buffer content as binary string
Public Function ReturnBinString(Optional ByVal Separator As String = " ") As StringExample:
Dim bin As String
bin = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBinString(" ")
' Result: 01001000 01100101Data Writing
SaveData
Write buffer to file
Public Function SaveData(Optional ByVal FilePath As String, Optional ByVal IsAppend As Boolean = False) As cFileExParameters:
FilePath- Target path (default uses OpenFile path)IsAppend- Whether to append (default False=overwrite)
Example:
' Write new file
VBMAN.FileEx.SetBufferText("Hello World", "UTF-8").SaveData "C:\\output.txt"
' Append to file
VBMAN.FileEx.SetBufferText("New Line" & vbCrLf, "UTF-8").SaveData "C:\\log.txt", TrueComprehensive Examples
Example 1: Quick Text File Reading
Private Function ReadTextFile(filePath As String, Optional charSet As String = "UTF-8") As String
On Error GoTo ErrorHandler
ReadTextFile = VBMAN.FileEx.OpenFile(filePath, "R").ReadData.ReturnText(charSet)
VBMAN.FileEx.CloseFile
Exit Function
ErrorHandler:
ReadTextFile = ""
End Function
Private Sub TestRead()
Dim content As String
content = ReadTextFile("C:\\data.txt", "UTF-8")
TextBox1.Text = content
End SubExample 2: Quick Text File Writing
Private Sub WriteTextFile(filePath As String, content As String, Optional charSet As String = "UTF-8")
On Error GoTo ErrorHandler
VBMAN.FileEx.SetBufferText(content, charSet).SaveData filePath
Exit Sub
ErrorHandler:
MsgBox "Write failed: " & Err.Description
End Sub
Private Sub TestWrite()
WriteTextFile "C:\\output.txt", TextBox1.Text, "UTF-8"
End SubExample 3: File Encoding Conversion
Private Sub ConvertFileEncoding(sourcePath As String, destPath As String, sourceCharset As String, destCharset As String)
' Read source file
Dim content As String
content = VBMAN.FileEx.OpenFile(sourcePath, "R").ReadData.ReturnText(sourceCharset)
VBMAN.FileEx.CloseFile
' Write destination file
VBMAN.FileEx.SetBufferText(content, destCharset).SaveData destPath
MsgBox "Conversion complete!"
End Sub
Private Sub TestConvert()
' GB2312 to UTF-8
ConvertFileEncoding "C:\\gbk.txt", "C:\\utf8.txt", "GB2312", "UTF-8"
End SubExample 4: Processing Binary Files
Private Sub ProcessBinaryFile()
' Read binary file
VBMAN.FileEx.OpenFile "C:\\data.bin", "R"
' Read first 16 bytes
Dim hexData As String
hexData = VBMAN.FileEx.ReadData(1, 16).ReturnHex(" ")
Debug.Print "Hex: " & hexData
' Read all and convert to Base64
Dim base64Data As String
base64Data = VBMAN.FileEx.ReadData.ReturnBase64
VBMAN.FileEx.CloseFile
' Save Base64 to text file
VBMAN.FileEx.SetBufferText(base64Data, "UTF-8").SaveData "C:\\base64.txt"
End SubExample 5: Large File Chunked Reading
Private Sub ReadLargeFile(filePath As String)
Const CHUNK_SIZE = 1048576 ' 1MB
VBMAN.FileEx.OpenFile filePath, "R"
Dim fileSize As Currency
fileSize = VBMAN.FileEx.FileSize
Dim totalRead As Currency
totalRead = 0
Do While totalRead < fileSize
Dim startPos As Long
Dim endPos As Long
startPos = totalRead + 1
endPos = totalRead + CHUNK_SIZE
If endPos > fileSize Then endPos = fileSize
' Read chunk of data
Dim chunk As String
chunk = VBMAN.FileEx.ReadData(startPos, endPos).ReturnText("UTF-8")
' Process this chunk...
ProcessChunk chunk
totalRead = endPos
' Show progress
Debug.Print "Progress: " & Int(totalRead / fileSize * 100) & "%"
DoEvents
Loop
VBMAN.FileEx.CloseFile
End SubExample 6: Log File Appending
Private Sub AppendLog(message As String)
Dim logEntry As String
logEntry = Format(Now, "yyyy-MM-dd hh:mm:ss") & " " & message & vbCrLf
VBMAN.FileEx.SetBufferText(logEntry, "UTF-8").SaveData App.Path & "\\app.log", True
End Sub
Private Sub TestLog()
AppendLog "Application started"
AppendLog "Execute operation A"
AppendLog "Execute operation B"
End SubBest Practices
- Close in Time: Call CloseFile promptly after file usage
- Error Handling: Add error handling mechanism for file operations
- Consistent Encoding: Use the same character encoding for reading and writing
- Chunk Large Files: Read/write large files exceeding memory capacity in chunks
- Path Checking: Check if file/directory exists before operation