Fs (API)

From ComputerCraft Wiki
Revision as of 03:07, 29 March 2013 by Superaxander (Talk | contribs)

Jump to: navigation, search

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


Grid disk.png   Fs (API)

Method NameDescription
fs.list(path) Lists the directories and files in the given directory
fs.exists(path) Checks if a path refers to an existing file or directory
fs.isDir(path) Checks if a path refers to an existing directory
fs.isReadOnly(path) Checks if a path is read-only (i.e. cannot be modified)
fs.getName(path) Gets the final component of a pathname
fs.getDrive(path) Gets the type of storage medium holding a file or directory
fs.getSize(path) Gets the size of a file and returns in bytes
fs.getFreeSpace(path) Gets the remaining space in the given directory.
fs.makeDir(path) Makes a directory
fs.move(fromPath, toPath) Moves a file or directory to a new location
fs.copy(fromPath, toPath) Copies a file or directory to a new location
fs.delete(path) Deletes a file or directory
fs.combine(basePath, localPath) Combines two path components, returning a path consisting of the local path nested inside the base path
fs.open(path, 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" is the same as making "/a/b/c". Leaving the slashes is just a matter of preference to the coder.