Difference between revisions of "Manual:File System Functions"

From Mudlet
Jump to navigation Jump to search
(Created page with "{{TOC right}} == File System Functions == A collection of functions for interacting with the file system. ===io.exists=== ; io.exists() : Checks to see if a given file or fol...")
 
Line 29: Line 29:
 
===lfs.attributes===
 
===lfs.attributes===
 
; lfs.attributes(path)
 
; lfs.attributes(path)
: Returns a table with detailed information regarding a file or directory, or nil if path is invalid.
+
: Returns a table with detailed information regarding a file or directory, or nil if path is invalid / file or folder does not exist.
  
 
;Example
 
;Example

Revision as of 03:46, 20 April 2013

File System Functions

A collection of functions for interacting with the file system.

io.exists

io.exists()
Checks to see if a given file or folder exists.
If it exists, it’ll return the Lua true boolean value, otherwise false.
Note: Behavior varies based on the user's operating system. Windows requires a specific file, while Linux will accept directories.
See lfs.attributes() for a cross-platform solution.
Example

<lua> -- This example works on Linux only if io.exists("/home/vadi/Desktop") then

 echo("This folder exists!")

else

 echo("This folder doesn't exist.")

end

-- This example will work on both Windows and Linux. if io.exists("/home/vadi/Desktop/file.tx") then

 echo("This file exists!")

else

 echo("This file doesn't exist.")

end </lua>

lfs.attributes

lfs.attributes(path)
Returns a table with detailed information regarding a file or directory, or nil if path is invalid / file or folder does not exist.
Example

<lua> fileInfo = lfs.attributes("/path/to/file_or_directory") if fileInfo then

   if fileInfo.mode == "directory" then
       echo("Path points to a directory.")
   elseif fileInfo.mode == "file" then
       echo("Path points to a file.")
   else
       echo("Path points to: "..fileInfo.mode)
   end
   display(fileInfo) -- to see the detailed information

else

   echo("The path is invalid (file/directory doesn't exist)")

end </lua>