metis.fs
Extends the CC's fs library with additional utility functions.
This module re-exports any function defined in fs, and so may shadow the fs global.
local fs = require "metis.fs" print(fs.exists("rom"))
| read_file(filename [, binary]) | Read a file into a string. |
|---|---|
| write_file(filename, contents [, binary]) | Write a file to a string. |
| walk(root, callback) | Walk a directory tree recursively, invoking a callback for every path found. |
| read_dirs(root) | Recursively read all files and folders in a directory. |
- read_file(filename [, binary])Source
Read a file into a string.
Parameters
- filename
stringThe file to read. - binary?
booleanWhether to read this as a binary file.
Returns
stringThe contents of the file.
Or
- nil If the file could not be read.
stringThe error which occurred when reading.
Usage
Read from
example.txt.local fs = require "metis.fs" print(fs.read_file("example.txt"))
- filename
- write_file(filename, contents [, binary])Source
Write a file to a string.
Parameters
- filename
stringThe file to read. - contents
stringThe contents of the file. - binary?
booleanWhether to write this as a binary file.
Throws
If the file could not be written to.
Usage
Write to
example.txt.local fs = require "metis.fs" fs.write_file("example.txt", "Hello, world!")
- filename
- walk(root, callback)Source
Walk a directory tree recursively, invoking a callback for every path found.
Parameters
- root
stringThe directory to start walking from. - callback function(name:
string, attributes:table):boolean| nilThe callback to invoke. This will be passed the file's absolute path, and the result of fs.attributes.
When given a directory, the function may return
falsein order to skip visiting its children.
Usage
- root
- read_dirs(root)Source
Recursively read all files and folders in a directory.
Parameters
- root
stringThe directory to start walking from.
Returns
- { [
string] =table} A mapping of absolute file/folder names to their attributes.
See also
walkFor finer-grain control.
- root