title: filelib section: filelib
A function that fails throws FileLibError (PermissionDenied for a path the
user may not read or write), so wrap calls that may fail in try / catch.
fileExists and isDir answer false for a path that does not exist.
filelib.writeFile(vpath, content)filelib.writeFile("user:/notes.txt", "hello");
filelib.readFile(vpath)var t = filelib.readFile("user:/notes.txt");
filelib.deleteFile(vpath)filelib.deleteFile("user:/notes.txt");
filelib.walk(vpath, mode)mode: all, file, folder
var allFiles = filelib.walk("user:/", "file");
filelib.glob(pattern, sortMode)sortMode supports default and user modes from FS sort settings.
var list = filelib.glob("user:/Desktop/*.jpg", "default");
filelib.aglob(pattern, sortMode)Advanced glob.
var list = filelib.aglob("user:/Desktop/**/*.png", "default");
filelib.readdir(vpath, sortMode)Returns array of objects: {Filename, Filepath, Ext, Filesize, Modtime, IsDir}.
var entries = filelib.readdir("user:/Desktop", "default");
filelib.filesize(vpath)var size = filelib.filesize("user:/movie.mp4");
filelib.fileExists(vpath)if (filelib.fileExists("user:/a.txt")) sendOK();
filelib.isDir(vpath)if (filelib.isDir("user:/Desktop")) sendOK();
filelib.mkdir(vpath)filelib.mkdir("user:/newfolder");
filelib.md5(vpath)var hash = filelib.md5("user:/a.txt");
filelib.mtime(vpath, parseToUnix)parseToUnix=true returns Unix timestamp; otherwise formatted string.
var ts = filelib.mtime("user:/a.txt", true);
filelib.rootName(vpath)Returns storage root display name.
var root = filelib.rootName("user:/Desktop/a.txt");
filelib.readBinaryFile(vpath, encoding)Returns the file's bytes as a string: encoding is "hex" (the default,
lowercase, two characters per byte) or "base64". Throws FileLibError when
the file does not exist. The whole file is loaded into memory, so keep it to
small files.
var hex = filelib.readBinaryFile("user:/icon.png");
var firstByte = parseInt(hex.substr(0, 2), 16);
//base64 goes straight into a data URL
var b64 = filelib.readBinaryFile("user:/icon.png", "base64");
sendResp("<img src='data:image/png;base64," + b64 + "'>");
filelib.writeBinaryFile(vpath, content, encoding)Decodes content and writes the bytes to the file, replacing it if it exists.
encoding is "hex" (the default; upper or lower case, even length) or
"base64" (a data: URL is accepted too). Returns true; throws
FileLibError when the content does not decode, the quota is full or the write
fails.
filelib.writeBinaryFile("user:/magic.bin", "89504e470d0a1a0a");
filelib.writeBinaryFile("user:/pixel.gif", "R0lGODlhAQABAAAAACw=", "base64");
//Copy any file byte for byte
filelib.writeBinaryFile("user:/copy.png", filelib.readBinaryFile("user:/icon.png", "base64"), "base64");