File SMFiles.mys
Files utilities, search, read/write into/from a table and some file format dedicated functions.
SMFiles.mys is automatically included when you Include "SMUtils".
- INI: a configuration file for software or script that consists of plain text with a structure and syntax comprising key–value pairs organized in sections.
- CSV: Comma-separated values data format. Each line is a record. Spreadsheet can import/export CSV files.
- Sylvain Machefert
Classes
| Class | Summary |
|---|---|
| CSV | The CSV class handles coma-separated data, stored in records (lines) of a plain text file. |
| INI | Rough INI configuration file reader and writer. |
| StructureReader | StructureReader is an object that can read some structured-text config or data files. |
Sections
Summary
| Return type | Function and summary |
|---|---|
| AddImagesDirectory(string path) Add a directory where script can search for pictures. | |
| table, int, int, int | CompareTextFiles(string file1, string file2) Compare two text files, returns a diff-like format. |
| CreateFolders(string fullPath) Create all folders that build a path. | |
| table | FileReadLines(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. | |
| table | GetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath, boolean caseInsensitive, boolean ignoreDiacritics, table allowedExtensions, int maxNbOfResults, boolean includeFolders, boolean foldersOnly)BrowseFolder(...) needs a callback function, and has a bug: here is a fixed and simplified version. |
| string | LocateImageFile(string fileName) Locate an image file in Harmony installation dir (system setting path). |
| string | ScriptReadVersion(string file) Read the version of a script. |
| StructureReader | StructureReader.loadFile(string fileName) Load a structured-text file. |
Functions
GetFilesInFolder(string folder, string mask, boolean recursive, boolean fullPath, boolean caseInsensitive, boolean ignoreDiacritics, table allowedExtensions, int maxNbOfResults, boolean includeFolders, boolean foldersOnly)
BrowseFolder(...) needs a callback function, and has a bug: here is a fixed and simplified version.
| Parameter | Type | Default | Description |
|---|---|---|---|
| folder | string | Folder name | |
| mask | string | "*.*" | Mask to search file, e.g. "*.myr", "a*.*" |
| recursive | boolean | false | true to browse sub-folders |
| fullPath | boolean | false | true returns the full path, false returns only file names |
| caseInsensitive | boolean | true on Windows, false else | Is the matching with mask case-insensitive?
|
| ignoreDiacritics | boolean | false | |
| allowedExtensions | table | Needed if the mask allow all extensions (e.g. "*.*", "*word*.*"), this argument allow to filter several extensions (e.g. "*.myr" and "*.pdf"). It may significantly speeds up the search. Expected format: {"myr","pdf"} | |
| maxNbOfResults | int | 25 | Stop searching after this limit is reached. Set to nil or 0 or negative number for no limit. |
| includeFolders | boolean | false | Include folder path in results? If true, returned path may not represnet a file. |
| foldersOnly | boolean | false | If true exclude files from results, returned paths represent only folders. |
- Return
- table: File names: array of string, in alphabetic order
CreateFolders(string fullPath)
Create all folders that build a path.
The MyrScript CreateFolder(...) (without ending 's') function doesn't create subfolders. This function (with an ending 's') call as many times as needed the CreateFolder(...).
Another way to explain: CreateFolders("C:/a/b") ensure that folder "a" is created in "C:", then next "b" is created in "a".
If fullPath already exist, nothing is done.
| Parameter | Type | Default | Description |
|---|---|---|---|
| fullPath | string | Full or relative path, e.g. "C:/a/b", "/path/to/folder/", "subfolder/123" |
- Return
- Error message as string or
0if no error.
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.
| Parameter | Type | Default | Description |
|---|---|---|---|
| path | string | The full path of the file to read. | |
| stringEncoding | int | UTF8_STR_ENCODING | String encoding constant from MSDefine, e.g. UNKNOWN_STR_ENCODING |
| crlf | int | MAC_CR | Carriage return constant from MSDefine, MAC_CR, LINUX_CR, WIN_CR, UNKNOWN_CR |
| errorIfNotFound | boolean | (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.
| Parameter | Type | Default | Description |
|---|---|---|---|
| path | string | The full path of the file to write | |
| lines | table | The lines to write, can be a table, a Collection or a Map | |
| formatFunc | function | nil | Formatting function that receive two arguments and must return a string that will be written into the file. Arguments are:
nil then table and Collection prints their values, Map prints key and values separated by a space char. |
| stringEncoding | int | UTF8_STR_ENCODING | String encoding constant from MSDefine, e.g. HTML_STR_ENCODING |
| crlf | int | MAC_CR | Carriage 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
| file | string | Script file name (or full path) |
- Return
- string: A version in the x.y.z or yyyyMMdd form (as source code), or
nilif file doesn't exist or version not found.
CompareTextFiles(string file1, string file2)
Compare two text files, returns a diff-like format.
The diff format is used for data comparison, or, in development environnement, to compare old and new lines of source code.
| Parameter | Type | Default | Description |
|---|---|---|---|
| file1 | string | First (generally oldest) file path to read | |
| file2 | string | Second (generally newest) file path to read |
- Returns
- table: Lines in approximate diff format:
- Added lines start with a + (plus)
- Removed lines start with a - (minus)
- Common lines (unchanged) start with a space.
- int: Number of added lines
- int: Number of removed lines
- int: Number of unchanged lines
- See
- CompareTables
- CompareStrings
LocateImageFile(string fileName)
Locate an image file in Harmony installation dir (system setting path).
This avoid embed image file in your script if they are copied from Harmony's documentation or cursors directory.
| Parameter | Type | Default | Description |
|---|---|---|---|
| fileName | string | fileName |
- Return
- string: The found file's full path, or
nilif not found.
AddImagesDirectory(string path)
Add a directory where script can search for pictures.
Menus and other Component may include image (picture) which are searched by file name in some Harmony's subdirs, but not in relative paths from the current script. If your script embed images, it is good practice to call AddImagesDirectory(GetCurrentScript()) just after Include "SMUtils".
| Parameter | Type | Default | Description |
|---|---|---|---|
| path | string | path |
StructureReader.loadFile(string fileName)
Load a structured-text file.
| Parameter | Type | Default | Description |
|---|---|---|---|
| fileName | string | fileName |
- Return
- StructureReader: StructureReader
- Error
- if fileName can't be read