Difference between revisions of "love.filesystem.remove"

(Add example for recursively removing files.)
m (Examples)
(6 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
ok = love.filesystem.remove( name )
+
success = love.filesystem.remove( name )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
 
{{param|string|name|The file or directory to remove.}}
 
{{param|string|name|The file or directory to remove.}}
 
=== Returns ===
 
=== Returns ===
{{param|boolean|ok|True if the file/directory was removed, false otherwise.}}
+
{{param|boolean|success|True if the file/directory was removed, false otherwise.}}
 
== 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.
 +
 +
If the file exists in the .love but not in the save directory, it returns <code>false</code> as well.
 +
 +
An opened [[File]] prevents removal of the underlying file. Simply close the [[File]] to remove it.
 +
 
== Examples ==
 
== 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.
 
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">
 
<source lang="lua">
 
function love.load()
 
function love.load()
     local dir = 'a';
+
     local dir = 'a'
 
     for _ = 1, 10 do
 
     for _ = 1, 10 do
         dir = dir .. '/a';
+
         dir = dir .. '/a'
 
     end
 
     end
     love.filesystem.createDirectory(dir);
+
     love.filesystem.createDirectory( dir )
 
end
 
end
  
 
function love.quit()
 
function love.quit()
     local function recursivelyDelete(item, depth)
+
     local function recursivelyDelete( item )
         if love.filesystem.isDirectory(item) then
+
         if love.filesystem.getInfo( item , "directory" ) then
             for _, child in pairs(love.filesystem.getDirectoryItems(item)) do
+
             for _, child in pairs( love.filesystem.getDirectoryItems( item )) do
                 recursivelyDelete(item .. '/' .. child, depth + 1);
+
                 recursivelyDelete( item .. '/' .. child )
                 love.filesystem.remove(item .. '/' .. child);
+
                 love.filesystem.remove( item .. '/' .. child )
 
             end
 
             end
         elseif love.filesystem.isFile(item) then
+
         elseif love.filesystem.getInfo( item ) then
             love.filesystem.remove(item);
+
             love.filesystem.remove( item )
 
         end
 
         end
         love.filesystem.remove(item)
+
         love.filesystem.remove( item )
 
     end
 
     end
 
+
     recursivelyDelete( 'a' )
     recursivelyDelete('a', 0);
 
 
end
 
end
 
</source>
 
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]

Revision as of 14:52, 16 October 2018

Removes a file or empty directory.

Function

Synopsis

success = love.filesystem.remove( name )

Arguments

string name
The file or directory to remove.

Returns

boolean success
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.

If the file exists in the .love but not in the save directory, it returns false as well.

An opened File prevents removal of the underlying file. Simply close the File to remove it.

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 )
        if love.filesystem.getInfo( item , "directory" ) then
            for _, child in pairs( love.filesystem.getDirectoryItems( item )) do
                recursivelyDelete( item .. '/' .. child )
                love.filesystem.remove( item .. '/' .. child )
            end
        elseif love.filesystem.getInfo( item ) then
            love.filesystem.remove( item )
        end
        love.filesystem.remove( item )
    end
    recursivelyDelete( 'a' )
end

See Also


Other Languages