Difference between revisions of "love.filesystem.remove"

m (Directory removal note)
(Add example for recursively removing files.)
Line 11: Line 11:
 
== Notes ==
 
== Notes ==
 
The directory must be empty before removal or else it will fail. Simply remove all files and folders in the directory beforehand.
 
The directory must be empty before removal or else it will fail. Simply remove all files and folders in the directory beforehand.
 +
== Examples ==
 +
Create a bunch of folders in the save folder and remove them and any file they may contain as soon as the game is quit.
 +
<source lang="lua">
 +
function love.load()
 +
    local dir = 'a';
 +
    for _ = 1, 10 do
 +
        dir = dir .. '/a';
 +
    end
 +
    love.filesystem.createDirectory(dir);
 +
end
 +
 +
function love.quit()
 +
    local function recursivelyDelete(item, depth)
 +
        if love.filesystem.isDirectory(item) then
 +
            for _, child in pairs(love.filesystem.getDirectoryItems(item)) do
 +
                recursivelyDelete(item .. '/' .. child, depth + 1);
 +
                love.filesystem.remove(item .. '/' .. child);
 +
            end
 +
        elseif love.filesystem.isFile(item) then
 +
            love.filesystem.remove(item);
 +
        end
 +
        love.filesystem.remove(item)
 +
    end
 +
 +
    recursivelyDelete('a', 0);
 +
end
 +
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]

Revision as of 20:17, 18 January 2015

Removes a file or empty directory.

Function

Synopsis

ok = love.filesystem.remove( name )

Arguments

string name
The file or directory to remove.

Returns

boolean ok
True if the file/directory was removed, false otherwise.

Notes

The directory must be empty before removal or else it will fail. Simply remove all files and folders in the directory beforehand.

Examples

Create a bunch of folders in the save folder and remove them and any file they may contain as soon as the game is quit.

function love.load()
    local dir = 'a';
    for _ = 1, 10 do
        dir = dir .. '/a';
    end
    love.filesystem.createDirectory(dir);
end
 
function love.quit()
    local function recursivelyDelete(item, depth)
        if love.filesystem.isDirectory(item) then
            for _, child in pairs(love.filesystem.getDirectoryItems(item)) do
                recursivelyDelete(item .. '/' .. child, depth + 1);
                love.filesystem.remove(item .. '/' .. child);
            end
        elseif love.filesystem.isFile(item) then
            love.filesystem.remove(item);
        end
        love.filesystem.remove(item)
    end
 
    recursivelyDelete('a', 0);
end

See Also


Other Languages