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
string
The file to read. - binary?
boolean
Whether to read this as a binary file.
Returns
string
The contents of the file.
Or
- nil If the file could not be read.
string
The 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
string
The file to read. - contents
string
The contents of the file. - binary?
boolean
Whether 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
string
The 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
false
in order to skip visiting its children.
Usage
- root
- read_dirs(root)Source
Recursively read all files and folders in a directory.
Parameters
- root
string
The directory to start walking from.
Returns
- { [
string
] =table
} A mapping of absolute file/folder names to their attributes.
See also
walk
For finer-grain control.
- root