Fs (API)

From ComputerCraft Wiki
Revision as of 19:43, 22 April 2013 by Hawk777 (Talk | contribs) (Copy descriptions from function pages, add return column, and use type template)

Jump to: navigation, search

The FS API allows you to mess around with the filesystem.


Grid disk.png   Fs (API)

ReturnMethod NameDescription
table files fs.list(string path) Returns a list of all the files (including subdirectories but not their contents) contained in a directory, as a numerically indexed table.
boolean exists fs.exists(string path) Checks if a path refers to an existing file or directory.
boolean dir fs.isDir(string path) Checks if a path refers to an existing directory.
boolean readOnly fs.isReadOnly(string path) Checks if a path is read-only (i.e. cannot be modified).
string name fs.getName(string path) Gets the final component of a pathname.
string drive, or nil fs.getDrive(string path) Returns the storage medium holding a path, or nil if the path does not exist.
int size fs.getSize(string path) Gets the size of a file in bytes
int space fs.getFreeSpace(string path) Gets the remaining space in the given directory.
nil fs.makeDir(string path) Makes a directory.
nil fs.move(string fromPath, string toPath) Moves a file or directory to a new location.
nil fs.copy(string fromPath, string toPath) Copies a file or directory to a new location.
nil fs.delete(string path) Deletes a file or directory.
string path fs.combine(string basePath, string localPath) Combines two path components, returning a path consisting of the local path nested inside the base path.
table handle fs.open(string path, string mode) Opens a file so it can be read or written.

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 nonempty 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.