fs (API)

From ComputerCraft Wiki
Jump to: navigation, search


The FS API allows you to manipulate files and the filesystem.

Grid disk.png  fs (API)
Function Return values Description
fs.list(string path) table files Returns a list of all the files (including subdirectories but not their contents) contained in a directory, as a numerically indexed table.
fs.exists(string path) boolean exists Checks if a path refers to an existing file or directory.
fs.isDir(string path) boolean isDirectory Checks if a path refers to an existing directory.
fs.isReadOnly(string path) boolean readonly Checks if a path is read-only (i.e. cannot be modified).
fs.getName(string path) string name Gets the final component of a pathname.
fs.getDrive(string path) string/nil drive Gets the storage medium holding a path, or nil if the path does not exist.
fs.getSize(string path) number size Gets the size of a file in bytes.
fs.getFreeSpace(string path) number space Gets the remaining space on the drive containing the given directory.
fs.makeDir(string path) nil Makes a directory.
fs.move(string fromPath, string toPath) nil Moves a file or directory to a new location.
fs.copy(string fromPath, string toPath) nil Copies a file or directory to a new location.
fs.delete(string path) nil Deletes a file or directory.
fs.combine(string basePath, string localPath) string path Combines two path components, returning a path consisting of the local path nested inside the base path.
fs.open(string path, string mode) table handle Opens a file so it can be read or written.
fs.find(string wildcard) table files Searches the computer's files using wildcards. Requires version 1.6 or later.
fs.getDir(string path) string parentDirectory Returns the parent directory of path. Requires version 1.63 or later.
fs.complete(string partial name, string path [, boolean include files] [, boolean include slashes]) table matches Returns a list of strings that could be combined with the provided name to produce valid entries in the specified folder. Requires version 1.74 or later.

Path Names

All of these functions except for fs.combine refer solely to absolute paths.
This means that the current working directory, as set by the cd command or the shell.setDir function, is ignored. Each path name consists of a list of non-empty path components separated by forward slashes, and those path components are taken one by one with the first being contained in the root directory of the computer.

If you need to deal with paths provided by the user that may be absolute or may be relative to the current working directory, use shell.resolve.

Unlike most real-world operating systems, ComputerCraft's absolute path name system does not need to be started with a forward slash ( / ), making the directory "a/b/c" the same as "/a/b/c". Leaving the slashes is just a matter of preference to the coder.