Difference between revisions of "Fs.list"

From ComputerCraft Wiki
Jump to: navigation, search
(Update description to correct semantics for non-directories and use type template)
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 +
{{lowercase}}
 
{{Function
 
{{Function
 
|name=fs.list
 
|name=fs.list
|args=[[string (type)|string]] path
+
|args={{Type|string}} path
 
|api=fs
 
|api=fs
|returns=[[table (type)|table]] list of files and folders in that path
+
|returns={{Type|table}} list of files and folders in <var>path</var>. If <var>path</var> is not a directory, an empty table is returned.
 +
|desc=Returns a list of all the files (including subdirectories but not their contents) contained in a directory, as a numerically indexed table.
 
|examples=
 
|examples=
 
{{Example
 
{{Example
 
|desc=Displays a list of all files and folders in the root directory of a computer
 
|desc=Displays a list of all files and folders in the root directory of a computer
|code=for k,v in ipairs(fs.list("/")) do
+
|code=local FileList = fs.list("") --Table with all the files and directories available
  print(v)
+
  end
+
for _, file in ipairs(FileList) do --Loop. Underscore because we don't use the key, ipairs so it's in order
|output=A list of all files and folders in /
+
  print(file) --Print the file name
 +
  end --End the loop
 +
 
 +
|output=A list of all files and folders in the root directory as a table. Note that fs.list() could also be inserted directly into ipairs() or pairs().
 
}}
 
}}
 
}}
 
}}
 +
 +
[[Category:Lua_Core_Functions]]

Latest revision as of 19:43, 22 April 2013


Grid Redstone.png  Function fs.list
Returns a list of all the files (including subdirectories but not their contents) contained in a directory, as a numerically indexed table.
Syntax fs.list(string path)
Returns table list of files and folders in path. If path is not a directory, an empty table is returned.
Part of ComputerCraft
API fs

Examples

Grid paper.png  Example
Displays a list of all files and folders in the root directory of a computer
Code
local FileList = fs.list("") --Table with all the files and directories available

for _, file in ipairs(FileList) do --Loop. Underscore because we don't use the key, ipairs so it's in order
  print(file) --Print the file name
end --End the loop
Output A list of all files and folders in the root directory as a table. Note that fs.list() could also be inserted directly into ipairs() or pairs().