slurpFile | reads the entire content of a file and returns it as a string. |
slurpFileCharSet | is basically the same as slurpFile, however, it allows to specify a character set when the file is slurped. |
flushToFile | is the opposite of slurpFile: it writes a string to a file. |
fileBaseName | returns a path (such as p:\ath\to\foo.txt) to the name of a file without suffix (in this case: foo). |
fileSuffix | returns the suffix of the file only (for example: txt). |
fileExists | checks if a given file exists. |
tempPath | uses the WinAPI function GetTempPath to return the folder into which temporary files can (should?) be written. |
File.bas uses the fileSystemObject of Microsoft's Scripting Runtime, thus, when used in a VBA project, the respective reference must be added, for example by executing the following statement in the immediate window: thisWorkbook.VBProject.references.AddFromGUID "{420B2830-E718-11CF-893D-00A0C9054228}" 1, 0
slurpFileCharSet was necessary because VBA is apparently unable to handle UTF-8 out of the box. loadFromFile method was needed (See also VBA's open statement with char sets). HKEY_CLASSES_ROOT\MIME\Database\Charset can be used for the value of the parameter charSet. '
' TODO 2020-02-25: with the implementation of file.cls, this file "File.bas" should probably be renamed.
'
option explicit
private declare ptrSafe function win32_GetTempPath lib "kernel32" alias "GetTempPathA" (byVal nBufferLength as long, byVal lpBuffer As string) as long
function slurpFile(fileName as string) as string ' {
'
' 2019-01-29: it turns out, VBA cannot really cope with
' reading utf 8 encoded files.
' Therefore, the ADODB substitue slurpFileCharSet might
' be considered instead.
'
dim f as integer
f = freeFile()
open fileName for input as #f
slurpFile = input(lof(f), #f)
close f
end function ' }
function slurpFileCharSet(fileName as string, optional charSet as string = "utf-8") as string ' {
'
' Read (slurp) a file in a specific charset.
'
' Needs the ADODB reference:
' call application.VBE.activeVBProject.references.addFromGuid("{B691E011-1797-432E-907A-4D8C69339129}", 6, 1)
'
dim s as new adodb.stream
s.charSet = charSet
s.open
s.loadFromFile(filename)
slurpFileCharSet = s.readText
s.close
end function ' }
sub flushToFile(filename as string, txt as string) ' {
dim f as integer
f = freeFile()
open fileName for output as #f
print# f, txt
close f
end sub ' }
function fileBaseName(filename as string) as string ' {
dim fso as new fileSystemObject
fileBaseName = fso.getBaseName(filename)
end function ' }
function fileSuffix(filename as string) as string ' {
dim fso as new fileSystemObject
fileSuffix = fso.getExtensionName(filename)
end function ' }
function fileExists(fileName as string) as boolean ' {
' http://stackoverflow.com/a/28237845/180275
on error resume next
fileExists = (getAttr(fileName) and vbDirectory) <> vbDirectory
end function ' }
public function tempPath() as string ' {
'
' 2019-01-11: It might probably be easier to
' just use:
' environ$("TEMP")
'
const MAX_PATH = 260
tempPath = string$(MAX_PATH, chr$(0))
win32_GetTempPath MAX_PATH, tempPath
tempPath = replace(tempPath, chr$(0), "")
end function ' }
option explicit
sub splitFile(fileName as string, chunkSize as long, optional overwrite as boolean = false)
dim buf() as byte
dim fnrIn as integer
dim fnrOut as integer
dim fileSize as long
dim pos as long
dim splitNr as long
on error goTo err_
fnrIn = freeFile
open fileName for binary access read as #fnrIn
fileSize = LOF(fnrIn)
reDim buf(1 to chunkSize)
splitNr = 0
for pos = 1 to fileSize step chunkSize
splitNr = splitNr + 1
if splitNr*chunkSize > fileSize then
reDim buf(1 to fileSize - (splitNr-1)*chunkSize)
end if
get #fnrIn, pos, buf
fnrOut = FreeFile
if overwrite then
if len(Dir$(fileName & "." & splitNr)) > 0 Then
kill fileName & "." & splitNr
end if
end if
open cStr(fileName & "." & splitNr) for binary access write as #fnrOut
put #fnrOut, , buf
close #fnrOut
next
close #fnrIn
exit sub
err_:
msgBox err.description
close #fnrIn
close #fnrOut
end sub
Filesystem and provide similar functionality like the PowerShell module filesystem.