Difference between revisions of "Fs.list"

From ComputerCraft Wiki
Jump to: navigation, search
(fixed code error.)
Line 5: Line 5:
 
|api=fs
 
|api=fs
 
|returns=[[table (type)|table]] list of files and folders in <var>path</var>, which must be a directory
 
|returns=[[table (type)|table]] list of files and folders in <var>path</var>, which must be a directory
−
|desc=Returns a list of all the files contained in a directory as a table
+
|desc=Returns a list of all the files contained in a directory, in table format
 
|examples=
 
|examples=
 
{{Example
 
{{Example
−
|desc=Displays a list of all files and folders in the root directory of a computer as a table
+
|desc=Displays a list of all files and folders in the root directory of a computer
−
|code=abc = fs.list("")
+
|code=local FileList = fs.list("") --Table with all the files and directories available
−
      i = 1
+
−
      while i ~= 15 do
+
for _,file in ipairs( FileList ) do --Loop. Underscore because we don't use the key, ipairs so it's in order
−
      print(abc[i])
+
  print( file ) --Print the file name
−
      i = i+1
+
end --End the loop
−
      end
+
  
 
|output=A list of all files and folders in the root directory as a table
 
|output=A list of all files and folders in the root directory as a table
 
}}
 
}}
 
}}
 
}}
−
 
−
abc = fs.list("")    <- will get all the files and directories in the system root into a table, all saved as 'abc'.
 
−
 
−
i = 1                <- sets i equal to one.
 
−
 
−
while i ~= 15 do    <- While i is not 15, continue to do this.
 
−
 
−
print(abc[i])        <- print the files and directories.
 
−
 
−
i = i+1              <- add 1 to i each run so a different file/directory gets listed next.
 
−
 
−
end                  <- end the loop.
 
−
 
−
 
−
this code will print 15 lines, blank or not, with the contents of the root directory.
 

Revision as of 11:46, 28 May 2012


Grid Redstone.png  Function fs.list
Returns a list of all the files contained in a directory, in table format
Syntax fs.list(string path)
Returns table list of files and folders in path, which must be a directory
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