Difference between revisions of "love.filesystem.getDirectoryItems"
Whatthefuck (talk | contribs) (→Function) |
m (→Recursively find and display all files and folders in a folder and its subfolders.: Don't use deprecated functions) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 14: | Line 14: | ||
=== Returns === | === Returns === | ||
{{param|table|files|A [[sequence]] with the names of all files and subdirectories as strings.}} | {{param|table|files|A [[sequence]] with the names of all files and subdirectories as strings.}} | ||
− | |||
− | |||
== Function == | == Function == | ||
− | {{ | + | {{newinoldin|[[0.9.1]]|091|[[0.10.0]]|100|type=variant}} |
=== Synopsis === | === Synopsis === | ||
<source lang="lua"> | <source lang="lua"> | ||
Line 48: | Line 46: | ||
-- in the folder and files in all subfolders | -- in the folder and files in all subfolders | ||
function recursiveEnumerate(folder, fileTree) | function recursiveEnumerate(folder, fileTree) | ||
− | local | + | local filesTable = love.filesystem.getDirectoryItems(folder) |
− | |||
for i,v in ipairs(filesTable) do | for i,v in ipairs(filesTable) do | ||
local file = folder.."/"..v | local file = folder.."/"..v | ||
− | + | local info = love.filesystem.getInfo(file) | |
− | fileTree = fileTree.."\n"..file | + | if info then |
− | + | if info.type == "file" then | |
− | + | fileTree = fileTree.."\n"..file | |
− | + | elseif info.type == "directory" then | |
+ | fileTree = fileTree.."\n"..file.." (DIR)" | ||
+ | fileTree = recursiveEnumerate(file, fileTree) | ||
+ | end | ||
end | end | ||
end | end | ||
Line 66: | Line 66: | ||
end | end | ||
</source> | </source> | ||
+ | |||
== See Also == | == See Also == | ||
* [[parent::love.filesystem]] | * [[parent::love.filesystem]] |
Latest revision as of 01:17, 24 August 2022
Available since LÖVE 0.9.0 |
It has been renamed from love.filesystem.enumerate. |
Returns a table with the names of files and subdirectories in the specified path. The table is not sorted in any way; the order is undefined.
If the path passed to the function exists in the game and the save directory, it will list the files and directories from both places.
Contents
Function
Synopsis
files = love.filesystem.getDirectoryItems( dir )
Arguments
string dir
- The directory.
Returns
Function
Available since LÖVE 0.9.1 and removed in LÖVE 0.10.0 |
This variant is not supported in earlier or later versions. |
Synopsis
files = love.filesystem.getDirectoryItems( dir, callback )
Arguments
string dir
- The directory.
function callback
- A function which is called for each file and folder in the directory. The filename is passed to the function as an argument.
Returns
Examples
Simple Example
local dir = ""
--assuming that our path is full of lovely files (it should at least contain main.lua in this case)
local files = love.filesystem.getDirectoryItems(dir)
for k, file in ipairs(files) do
print(k .. ". " .. file) --outputs something like "1. main.lua"
end
Recursively find and display all files and folders in a folder and its subfolders.
function love.load()
filesString = recursiveEnumerate("", "")
end
-- This function will return a string filetree of all files
-- in the folder and files in all subfolders
function recursiveEnumerate(folder, fileTree)
local filesTable = love.filesystem.getDirectoryItems(folder)
for i,v in ipairs(filesTable) do
local file = folder.."/"..v
local info = love.filesystem.getInfo(file)
if info then
if info.type == "file" then
fileTree = fileTree.."\n"..file
elseif info.type == "directory" then
fileTree = fileTree.."\n"..file.." (DIR)"
fileTree = recursiveEnumerate(file, fileTree)
end
end
end
return fileTree
end
function love.draw()
love.graphics.print(filesString, 0, 0)
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info