Difference between revisions of "love.filesystem.enumerate"

m
(Added simple example.)
Line 13: Line 13:
 
{{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.}}
 
== Examples ==
 
== Examples ==
 +
=== Simple Example ===
 +
<source lang="lua">
 +
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.enumerate(dir)
 +
for k, file in ipairs(files) do
 +
print(k .. ". " .. file) --outputs something like "1. main.lua"
 +
end
 +
</source>
 
=== Recursively find and display all files and folders in a folder and its subfolders. ===
 
=== Recursively find and display all files and folders in a folder and its subfolders. ===
 
<source lang="lua">
 
<source lang="lua">

Revision as of 20:49, 13 September 2012

Returns a table with the names of files and subdirectories in the directory in an undefined order.

Note that this directory is relative to the love folder/archive being run. Absolute paths will not work.

Function

Synopsis

files = love.filesystem.enumerate( dir )

Arguments

string dir
The directory.

Returns

table files
A sequence with the names of all files and subdirectories as strings.

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.enumerate(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 lfs = love.filesystem
	local filesTable = lfs.enumerate(folder)
	for i,v in ipairs(filesTable) do
		local file = folder.."/"..v
		if lfs.isFile(file) then
			fileTree = fileTree.."\n"..file
		elseif lfs.isDirectory(file) then
			fileTree = fileTree.."\n"..file.." (DIR)"
			fileTree = recursiveEnumerate(file, fileTree)
		end
	end
	return fileTree
end
	
function love.draw()
	love.graphics.print(filesString, 0, 0)
end

See Also


Other Languages