fhFileUtils
Reference for the fhFileUtils module, a set of functions for Family Historian
plugins to work with extended-character-set file and folder names, and to read and write text
files with extended names and extended-character-set contents (using Windows scripting via
Scripting.FileSystemObject).
- Version: 1.9
- Author: Calico Pie
- Contributors: Jane Taubman, Helen Wright
- Licence: MIT (see plugin licence)
- Dependencies:
luacom
All functions below are accessed via the fhFileUtils table, e.g. fhFileUtils.fileExists(...).
Error convention: every function that can fail returns two values: the result (or false on
failure) and an error string on failure. The specific error strings a function can return are
listed per function below.
Emulator note: functions that read file dates, sizes or attributes are not supported when
running under an emulator (e.g. Crossover) and will return an error in that case — see
getFolderContents, getDateModified, getDateCreated, getFileFolderDetails.
Contents
- Existence & path checks
- Folder operations
- File operations
- Text file I/O
- Binary file I/O
- File/folder details & dates
- Attributes
- Path utilities
Existence & path checks
fhFileUtils.folderExists(sPath)
- Parameters:
sPath— full folder path - Returns:
boolean—trueif the folder exists
fhFileUtils.fileExists(sPath)
- Parameters:
sPath— full file path - Returns:
boolean—trueif the file exists
fhFileUtils.getParent(sPath)
Returns the parent folder for the given path. The path itself is not validated.
- Parameters:
sPath— full file or folder path - Returns: parent folder path, or
falseon failure - Errors:
'Path not found'
Folder operations
fhFileUtils.getFolderContents(sPath, bRecurse, bExtend)
Returns a table of all files and folders in the specified folder. For each item, path,
created, modified, size and attributes are included.
- Parameters:
sPath— full folder pathbRecurse— recurse into subfolders; defaults tofalsebExtend— return extended details (dates, size, attributes); defaults totrue. Iffalse(required in emulator environments), dates/attributes are set to""and size to0
- Returns: table of results, or
falseon failure - Errors:
'Folder not found','Extended details not supported in an emulator','Operation failed'(with luacom code)
fhFileUtils.createFolder(sPath)
- Parameters:
sPath— full folder path - Returns:
trueif created, orfalseon failure - Errors:
'Parent folder not found','Bad folder name','Folder already exists','Operation failed'(with luacom code)
fhFileUtils.deleteFolder(sPath, bForce)
Deletes a folder and all its contents.
- Parameters:
sPath— full folder pathbForce— delete even if it contains read-only files; defaults tofalse
- Returns:
trueif deleted, orfalseon failure - Errors:
'Folder not found','Operation failed'(with luacom code)
fhFileUtils.renameFolder(sPath, sNewName)
Renames a folder in its current location.
- Parameters:
sPath— full folder path;sNewName— new folder name - Returns:
trueif renamed, orfalseon failure - Errors:
'Folder not found','Folder already exists','Bad folder name','Operation failed'(with luacom code)
fhFileUtils.copyFolder(sPath, sDestination)
Copies a folder as a sub-folder of the destination folder. Will not copy on top of an existing folder.
- Parameters:
sPath— full folder path to copy;sDestination— full destination folder path - Returns:
trueif copied, orfalseon failure - Errors:
'Folder not found','Destination folder not found','Folder already exists','Operation failed'(with luacom code)
fhFileUtils.moveFolder(sPath, sDestination)
- Parameters:
sPath— full folder path;sDestination— full destination folder path - Returns:
trueif moved, orfalseon failure - Errors:
'Folder not found','Destination folder not found','Folder already exists','Operation failed'(with luacom code)
File operations
fhFileUtils.deleteFile(sPath, bForce)
- Parameters:
sPath— full file pathbForce— delete even if read-only; defaults tofalse
- Returns:
trueif deleted, orfalseon failure - Errors:
'Read-only file','File not found','Operation failed'(with luacom code)
fhFileUtils.renameFile(sPath, sNewName)
Renames a file in its current location.
- Parameters:
sPath— full file path;sNewName— new file name including extension - Returns:
trueif renamed, orfalseon failure - Errors:
'File not found','File already exists','Bad file name','Operation failed'(with luacom code)
fhFileUtils.copyFile(sPath, sDestination, bOverwrite)
- Parameters:
sPath— full file path to copysDestination— full file or folder path (folders must have a trailing path separator)bOverwrite— overwrite an existing file; defaults tofalse. A read-only file can never be overwritten
- Returns:
trueif copied, orfalseon failure - Errors:
'File not found','Destination folder not found','File already exists','Operation failed'(with luacom code)
fhFileUtils.moveFile(sPath, sDestination)
- Parameters:
sPath— full file pathsDestination— full file or folder path (folders must have a trailing path separator)
- Returns:
trueif moved, orfalseon failure - Errors:
'File not found','Destination folder not found','File already exists','Operation failed'(with luacom code)
Text file I/O
fhFileUtils.createTextFile(sPath, bOverwrite, bUnicode, sContents, iBits)
Creates a text file with optional contents.
- Parameters:
sPath— full file pathbOverwrite— overwrite an existing file; defaults tofalsebUnicode— create a Unicode file rather than ANSI; defaults tofalsesContents— contents to write; defaults to""iBits—8for UTF-8 or16for UTF-16; defaults to8.16requiresbUnicode = true
- Returns:
trueif created, orfalseon failure - Errors:
'Bad parameters'(e.g.iBits == 16withoutbUnicode),'Destination folder not found','Bad file name','File already exists','Operation failed'
fhFileUtils.readTextFile(sPath, bUnicode, iBits)
Reads an existing text file. No checks are made that the file is actually in the specified format.
- Parameters:
sPath— full file pathbUnicode— file is Unicode rather than ANSI; defaults tofalseiBits—8for UTF-8 or16for UTF-16; defaults to8
- Returns: file contents as a string, or
falseon failure - Errors:
'File not found','Bad parameters','Operation failed'
Binary file I/O
fhFileUtils.fileGetContents(sFileName)
Loads the contents of any existing file into a string, reading it as binary — safe for graphics
or other binary files. For text files, prefer readTextFile.
- Parameters:
sFileName— filename with path - Returns: file contents as a string, or
falseon failure - Errors:
'File not found','Operation failed'(with luacom code)
fhFileUtils.filePutContents(sFileName, sContents, bOverwrite)
Saves a string to a file, writing it as binary — safe for graphics or other binary files. For
text files, prefer createTextFile.
- Parameters:
sFileName— filename with pathsContents— string to writebOverwrite— overwrite an existing file; defaults tofalse. A read-only file can never be overwritten
- Returns:
trueif written, orfalseon failure - Errors:
'Destination folder not found','Bad file name','File already exists','Operation failed'
File/folder details & dates
fhFileUtils.getDateModified(sPath)
- Parameters:
sPath— full file (or folder) path - Returns: modification date as UNIX epoch seconds, or
falseon failure - Errors:
'Modified date not supported in an emulator','File or folder not found','Operation failed'(with luacom code)
fhFileUtils.getDateCreated(sPath)
- Parameters:
sPath— full file (or folder) path - Returns: creation date as UNIX epoch seconds, or
falseon failure - Errors:
'Created date not supported in an emulator','File or folder not found','Operation failed'(with luacom code)
fhFileUtils.getFileFolderDetails(sPath, bExtend)
Returns details for a single file or folder: path, created/createdepoch,
modified/modifiedepoch, size and attributes.
- Parameters:
sPath— full file or folder pathbExtend— return extended details (dates, size, attributes); defaults totrue. Iffalse(required in emulator environments), dates/attributes are set to""and size to0
- Returns: table of results, or
falseon failure - Errors:
'Extended details not available in emulator','File or folder not found','Operation failed'(with luacom code)
Attributes
fhFileUtils.isFileReadOnly(sPath)
- Parameters:
sPath— full file path - Returns:
"Read only"or"Read write", orfalseon failure - Errors:
'File not found','Operation failed'(with luacom code)
fhFileUtils.setFileReadOnly(sPath, setReadOnly)
Sets or clears a file's read-only attribute.
- Parameters:
sPath— full file path;setReadOnly—trueto set read-only,falseto clear it - Returns:
trueif successful, orfalseon failure - Errors:
'File not found','Operation failed'(with luacom code)
Path utilities
fhFileUtils.splitPath(sPath)
Splits a path into its component parts. The path is not validated, only split — except that for
a network share path (\\computer\share) the share must exist for drive to be returned.
- Parameters:
sPath— full file or folder path - Returns: table with
drive,abs_path(fully resolved path),parent,filename(excluding path),basename(excluding path and extension),ext
fhFileUtils.buildPath(sFolderPath, sAppend)
Builds a path string from a folder path and a filename or sub-path. Does not validate the input or the result.
- Parameters:
sFolderPath— full path to a folder;sAppend— file or folder name to append - Returns: constructed path string
Internal helpers (not exported)
Used internally by the module but not part of the fhFileUtils public table:
saveLuacomErrorHandling()/testLuacomErrorState()— disable luacom's abort-on-error around a COM call, then report whether it succeedednameValid(sName)— checks a file/folder name doesn't contain invalid characters (<>:"/\|?*)fileReadOnly(sPath)— checks the read-only attribute bit on a filegetName(sPath)— returns the file name portion of a pathgetDetails(f, bExtend)— builds the common{name, created, path, size, modified, attributes}table used bygetFolderContentsandgetFileFolderDetailsEpochTime(dDate)— converts a COM date object to UNIX epoch seconds