Difference between revisions of "Manual:File System Functions"
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...") |
(→File System Functions: note about LFS) |
||
(8 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{TOC right}} | {{TOC right}} | ||
− | + | {{#description2:Mudlet API documentation for functions interacting with the file system.}} | |
+ | = File System Functions = | ||
A collection of functions for interacting with the file system. | A collection of functions for interacting with the file system. | ||
− | + | {{note}} Mudlet also includes LuaFileSystem. See their [https://lunarmodules.github.io/luafilesystem/manual.html manual] for more details. | |
− | ; io.exists() | + | |
− | : Checks to see if a given file or folder exists. | + | ==io.exists== |
+ | ; io.exists(path) | ||
+ | : Checks to see if a given file or folder exists. | ||
: If it exists, it’ll return the Lua true boolean value, otherwise false. | : If it exists, it’ll return the Lua true boolean value, otherwise false. | ||
− | |||
: See [[Manual:Miscellaneous_Functions#lfs.attributes|lfs.attributes()]] for a cross-platform solution. | : See [[Manual:Miscellaneous_Functions#lfs.attributes|lfs.attributes()]] for a cross-platform solution. | ||
;Example | ;Example | ||
− | <lua> | + | <syntaxhighlight lang="lua"> |
-- This example works on Linux only | -- This example works on Linux only | ||
if io.exists("/home/vadi/Desktop") then | if io.exists("/home/vadi/Desktop") then | ||
Line 25: | Line 27: | ||
echo("This file doesn't exist.") | echo("This file doesn't exist.") | ||
end | end | ||
− | </ | + | </syntaxhighlight> |
− | + | ==lfs.attributes== | |
− | ; lfs.attributes(path) | + | ; infoTable = 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 | ||
− | <lua> | + | <syntaxhighlight lang="lua"> |
fileInfo = lfs.attributes("/path/to/file_or_directory") | fileInfo = lfs.attributes("/path/to/file_or_directory") | ||
if fileInfo then | if fileInfo then | ||
Line 46: | Line 48: | ||
echo("The path is invalid (file/directory doesn't exist)") | echo("The path is invalid (file/directory doesn't exist)") | ||
end | end | ||
− | </ | + | </syntaxhighlight> |
[[Category:Mudlet API]] | [[Category:Mudlet API]] | ||
[[Category:Mudlet Manual]] | [[Category:Mudlet Manual]] |
Latest revision as of 07:52, 4 May 2024
File System Functions
A collection of functions for interacting with the file system.
Note: Mudlet also includes LuaFileSystem. See their manual for more details.
io.exists
- io.exists(path)
- Checks to see if a given file or folder exists.
- If it exists, it’ll return the Lua true boolean value, otherwise false.
- See lfs.attributes() for a cross-platform solution.
- Example
-- 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
lfs.attributes
- infoTable = 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
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