Difference between revisions of "Fs (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(Parameter fixes)
(Writeup about path names)
Line 41: Line 41:
 
|}
 
|}
 
[[Category:APIs]]
 
[[Category:APIs]]
 +
 +
== Path Names ==
 +
 +
All of these functions except for [[fs.combine]] refer solely to <em>absolute paths</em>. This means that the current working directory, as set by the <code>cd</code> 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 canonical form absolute pathnames do not begin with a slash: <code>a/b/c</code> is the canonical form of an absolute pathname for the file or directory <code>c</code> contained in the directory <code>b</code>, itself contained in the directory <code>a</code>, itself contained in the root directory. The empty string, <code>""</code>, refers to the root directory when using these functions. One can observe this canonicalization rule in effect with the [[fs.combine]] function; even if a leading slash is present in either (or both) of the path components to combine, the resulting path will not have a leading slash. However, these functions also work with paths that have leading slashes (for example, [[fs.list]]("/") returns the same as [[fs.list]]("")), so leave them in if you prefer.

Revision as of 03:54, 12 March 2012

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

Method name Description
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.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 canonical form absolute pathnames do not begin with a slash: a/b/c is the canonical form of an absolute pathname for the file or directory c contained in the directory b, itself contained in the directory a, itself contained in the root directory. The empty string, "", refers to the root directory when using these functions. One can observe this canonicalization rule in effect with the fs.combine function; even if a leading slash is present in either (or both) of the path components to combine, the resulting path will not have a leading slash. However, these functions also work with paths that have leading slashes (for example, fs.list("/") returns the same as fs.list("")), so leave them in if you prefer.