Section Files

Files utility functions

Class

ClassSummary
INIRough INI configuration file reader and writer.

Summary

Return typeFunction and summary
tableFileReadLines(string path, int stringEncoding, int crlf, boolean errorIfNotFound)
Syntaxic sugar to read all lines of a file, and return a table of them.
 FileWriteLines(string path, table lines, function formatFunc, int stringEncoding, int crlf)
Syntaxic sugar to writes a table of strings as lines in a file.
tableGetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath)
BrowseFolder(...) needs a callback function, and has a bug: here is a fixed and simplified version.
stringScriptReadVersion(string file)
Read the version of a script.
string, stringSplitPath(string path)
Split a path into directory and file, no file if path ends by a '/'.

Functions

GetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath)

BrowseFolder(...) needs a callback function, and has a bug: here is a fixed and simplified version.

ParameterTypeDefaultDescription
folderstring Folder name
maskstring"*.*"Mask to search file, e.g. "*.myr", "a*.*"
recursivebooleanfalsetrue to browse sub-folders
fullPathbooleanfalsetrue returns the full path, false returns only file names
Return
table: File names: array of string, in alphabetic order

FileReadLines(string path, int stringEncoding, int crlf, boolean errorIfNotFound)

Syntaxic sugar to read all lines of a file, and return a table of them.

Note:

  • encoding is supposed to be UTF-8 BOM and lines terminated by Macintosh CR.
  • Convenient for small files when full content have to been read and stored.

ParameterTypeDefaultDescription
pathstring The full path of the file to read.
stringEncodingintUTF8_STR_ENCODINGString encoding constant from MSDefine, e.g. UNKNOWN_STR_ENCODING
crlfintMAC_CRCarriage return constant from MSDefine, MAC_CR, LINUX_CR, WIN_CR, UNKNOWN_CR
errorIfNotFoundboolean (defaut=true) Throw error if file is not found, else return an empty table.
Return
table: Table of string, one item per line
Errors
if argument is not a string
if file can't be read
See
FileWriteLines
Example


 local lines = FileReadLines("/path/to/myConfigFile.txt")
 print(lines[1]) -- first line of the config file

FileWriteLines(string path, table lines, function formatFunc, int stringEncoding, int crlf)

Syntaxic sugar to writes a table of strings as lines in a file.

Collection and Map are also handled, and an optional formating function can transform lines before writing. Notes:

  • The file is overwritten,
  • Encoding is UTF-8 BOM and lines are terminated by Macintosh CR.

ParameterTypeDefaultDescription
pathstring The full path of the file to write
linestable The lines to write, can be a table, a Collection or a Map
formatFuncfunctionnilFormatting function that receive two arguments and must return a string that will be written into the file. Arguments are:
  • if simple table: int table index and the value,
  • if Collection: a int counter and a Collection item,
  • if Map, key and value (an entry of the Map).
If formatFunc is nil then table and Collection prints their values, Map prints key and values separated by a space char.
stringEncodingintUTF8_STR_ENCODINGString encoding constant from MSDefine, e.g. HTML_STR_ENCODING
crlfintMAC_CRCarriage return constant from MSDefine, MAC_CR, LINUX_CR, WIN_CR, UNKNOWN_CR
Errors
if arguments have bad type.
if file can't be written.
See
FileReadLines
Examples


 local lines = FileReadLines("/path/to/myConfigFile.txt")
 lines[1] = "0" -- change a value in the config file
 FileWriteLines("/path/to/myConfigFile.txt", lines)


 local map = Map:new("number", "string", SORT_ASCENDING)
 map:put(1, "one"); map:put(2, "two"); map:put(3, "three")
  FileWriteLines("/path/to/file/count.txt", map,
   function(key,value) return tostring(key).."="..strupper(value); end)
 )
 -- outputs 3 lines 1=ONE, 2=TWO and 3=THREE

ScriptReadVersion(string file)

Read the version of a script.

Look for the --VERSION: x.y in first 30 lines of source code.

ParameterTypeDefaultDescription
filestring Script file name (or full path)
Return
string: A version in the x.y.z or yyyyMMdd form (as source code), or nil if file doesn't exist or version not found.

SplitPath(string path)

Split a path into directory and file, no file if path ends by a '/'.

ParameterTypeDefaultDescription
pathstring path
Returns
string: directory
string: file name