Difference between revisions of "Manual:File System Functions"

From Mudlet
Jump to navigation Jump to search
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{TOC right}}
 
{{TOC right}}
== File System Functions ==
+
{{#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.
  
===io.exists===
+
{{note}} Mudlet also includes LuaFileSystem.  See their [https://lunarmodules.github.io/luafilesystem/manual.html manual] for more details.
; io.exists()
+
 
 +
==io.exists==
 +
; io.exists(path)
 
: Checks to see if a given file or folder exists.
 
: 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.
Line 26: Line 29:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===lfs.attributes===
+
==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 / file or folder does not exist.
 
: Returns a table with detailed information regarding a file or directory, or nil if path is invalid / file or folder does not exist.
  
Line 49: Line 52:
 
[[Category:Mudlet API]]
 
[[Category:Mudlet API]]
 
[[Category:Mudlet Manual]]
 
[[Category:Mudlet Manual]]
 +
 +
==openMudletHomeDir==
 +
;openMudletHomeDir()
 +
 +
:Opens the current home directory of the current profile in your local system's file browser. This can be used to review or modify data stored there.
 +
 +
{{MudletVersion|4.20}}
 +
 +
;See also:[[Manual:Lua_Functions#getMudletHomeDir|getMudletHomeDir()]]
 +
 +
;Example
 +
<syntaxhighlight lang="lua">
 +
openMudletHomeDir()
 +
</syntaxhighlight>
 +
 +
==saveProfile==
 +
;saveProfile([location, [filename]])
 +
 +
:Saves the current Mudlet profile to disk, which is equivalent to pressing the "Save Profile" button.
 +
 +
{{MudletVersion|4.20}}
 +
 +
;Parameters
 +
* ''location:''
 +
:(optional) folder to save the profile to. If not given, the profile will go into the [[Mudlet_File_Locations|default location]].
 +
 +
* ''filename:''
 +
:(optional) save the profile as the filename. If the filename doesn't end with ''.xml'' this function will automatically save it as ''filename.xml''. If not given, the profile will be saved with the default ''DATE#TIME.xml'' filename format.
 +
 +
;Example
 +
<syntaxhighlight lang="lua">
 +
-- save profile to the .../mudlet/profiles/profileName/current folder
 +
saveProfile()
 +
 +
-- save to the desktop on Windows:
 +
saveProfile([[C:\Users\yourusername\Desktop]])
 +
 +
-- alternately, save to the desktop with the name system.xml:
 +
saveProfile([[C:\Users\yourusername\Desktop]],"system")
 +
</syntaxhighlight>

Latest revision as of 02:51, 4 February 2026

File System Functions

A collection of functions for interacting with the file system.

Note 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

openMudletHomeDir

openMudletHomeDir()
Opens the current home directory of the current profile in your local system's file browser. This can be used to review or modify data stored there.
Mudlet VersionAvailable in Mudlet4.20+
See also
getMudletHomeDir()
Example
openMudletHomeDir()

saveProfile

saveProfile([location, [filename]])
Saves the current Mudlet profile to disk, which is equivalent to pressing the "Save Profile" button.
Mudlet VersionAvailable in Mudlet4.20+
Parameters
  • location:
(optional) folder to save the profile to. If not given, the profile will go into the default location.
  • filename:
(optional) save the profile as the filename. If the filename doesn't end with .xml this function will automatically save it as filename.xml. If not given, the profile will be saved with the default DATE#TIME.xml filename format.
Example
-- save profile to the .../mudlet/profiles/profileName/current folder
saveProfile()

-- save to the desktop on Windows:
saveProfile([[C:\Users\yourusername\Desktop]])

-- alternately, save to the desktop with the name system.xml:
saveProfile([[C:\Users\yourusername\Desktop]],"system")