os.rename()

Type Function
Library os.*
Return value Boolean and String
Revision Release 2024.3703
Keywords rename, file
See also system.pathForFile()
os.remove()
io.*

Overview

Renames a file or directory and returns two possible values:

Gotchas

You can only rename files in the DocumentsDirectory, ApplicationSupportDirectory and TemporaryDirectory. Files in the ResourceDirectory are read-only.

Syntax

os.rename( oldname, newname )
oldname (required)

String. Old file name.

newname (required)

String. New file name.

Example

local destDir = system.DocumentsDirectory  -- Location where the file is stored
local result, reason = os.rename(
    system.pathForFile( "orange.txt", destDir ),
    system.pathForFile( "apple.txt", destDir )
)
 
if result then
    print( "File renamed" )
else
    print( "File not renamed", reason )  --> File not renamed    orange.txt: No such file or directory
end