Difference between revisions of "Fs (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Fixed table row background colors and adjusted column widths.)
(Changing int to number)
Line 34: Line 34:
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the storage medium holding a path, or [[nil]] if the path does not exist.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the storage medium holding a path, or [[nil]] if the path does not exist.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|int}} size</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|number}} size</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getSize]]({{Type|string}} path)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getSize]]({{Type|string}} path)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the size of a file in bytes</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the size of a file in bytes</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|int}} space</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|number}} space</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getFreeSpace]]({{Type|string}} path)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getFreeSpace]]({{Type|string}} path)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the remaining space in the given directory.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the remaining space in the given directory.</td></tr>

Revision as of 10:47, 18 July 2013

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.
number size fs.getSize(string path) Gets the size of a file in bytes
number 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.