Difference between revisions of "Fs (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Path Names: Grammar)
(Copy descriptions from function pages, add return column, and use type template)
Line 3: Line 3:
  
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
<tr><td colspan="2" style="font-weight: bold; font-size: large; padding-bottom: .3em; border-bottom: solid #C9C9C9 1px; background: #D3FFC2; line-height:28px;">
+
<tr><td colspan="3" style="font-weight: bold; font-size: large; padding-bottom: .3em; border-bottom: solid #C9C9C9 1px; background: #D3FFC2; line-height:28px;">
 
[[File:Grid_disk.png|24px]]&nbsp;&nbsp;
 
[[File:Grid_disk.png|24px]]&nbsp;&nbsp;
 
Fs (API)
 
Fs (API)
 
</td></tr>
 
</td></tr>
  
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
+
<tr><td style="width: 100px; background: #E0E0E0; padding: .4em; font-weight:bold;">Return</td><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.list]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|table}} files</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Lists the directories and files in the given directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.list]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns a list of all the files (including subdirectories but not their contents) contained in a directory, as a numerically indexed table.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.exists]](path)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|boolean}} exists</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path refers to an existing file or directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.exists]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path refers to an existing file or directory.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.isDir]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|boolean}} dir</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path refers to an existing directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.isDir]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path refers to an existing directory.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.isReadOnly]](path)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|boolean}} readOnly</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path is read-only (i.e. cannot be modified)</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.isReadOnly]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks if a path is read-only (i.e. cannot be modified).</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getName]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|string}} name</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the final component of a pathname</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getName]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the final component of a pathname.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getDrive]](path)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|string}} drive, or [[nil]]</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the type of storage medium holding a file or directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getDrive]]({{Type|string}} path)</td>
 +
<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;">[[fs.getSize]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|int}} size</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Gets the size of a file and returns in bytes</td></tr>
+
<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>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.getFreeSpace]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|int}} 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;">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>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.makeDir]](path)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Makes a directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.makeDir]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Makes a directory.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.move]](fromPath, toPath)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Moves a file or directory to a new location</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.move]]({{Type|string}} fromPath, {{Type|string}} toPath)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Moves a file or directory to a new location.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.copy]](fromPath, toPath)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Copies a file or directory to a new location</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.copy]]({{Type|string}} fromPath, {{Type|string}} toPath)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Copies a file or directory to a new location.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.delete]](path)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Deletes a file or directory</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.delete]]({{Type|string}} path)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Deletes a file or directory.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.combine]](basePath, localPath)</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|string}} path</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Combines two path components, returning a path consisting of the local path nested inside the base path</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.combine]]({{Type|string}} basePath, {{Type|string}} localPath)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Combines two path components, returning a path consisting of the local path nested inside the base path.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.open]](path, mode)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{Type|table}} handle</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Opens a file so it can be read or written</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[fs.open]]({{Type|string}} path, {{Type|string}} mode)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Opens a file so it can be read or written.</td></tr>
 
</table>
 
</table>
  

Revision as of 19:43, 22 April 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.
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.