FS API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionfs.combine
Concatenates path pathA with path pathB, such that pathB appears after pathA. A naïve implementation of this function would be pathA .. "/" .. pathB. fs.combine should always be preferred over such a pattern, as this function will take care of special cases, such as when pathB starts with a forward slash, and typeA ends with a forward slash. The naïve implementation would generate three forward slashes between the paths.
Syntax fs.combine()
Returns string
Part of CC:Tweaked (source)
API fs
ExampleExampleCreating a path to a directory within the ROM directory
Prints the combination of the paths /rom and /dir/test.
Code
print(fs.combine("/rom", "/dir/test"))
Output rom/dir/test

Fs.complete

FunctionFunctionfs.copy
Copies the file at the path path to the path dest. If dest is a directory, the function will copy the file into that directory with its original name.
Syntax fs.copy()
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleExampleCopy a file to a directory
Copies the edit program (/rom/programs/edit.lua) to the root directory (/).
Code
fs.copy("/rom/programs/edit.lua", "/")
Output A copy of the edit program will now be located at /edit.lua.
ExampleExampleCopy a file with a new name
Copies the clear program (/rom/programs/clear.lua) to /cls.lua.
Code
fs.copy("/rom/programs/clear.lua", "/cls.lua")
Output A copy of the clear program will now be located at /cls.lua


FunctionFunctionfs.delete
Deletes the file or directory at the path path. If path is a directory, it will delete the directory as well as all the files inside.
Warning Warning: This function is irreversible! Make sure you enter the arguments carefully so that you do not delete anything important.
Syntax fs.delete()
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleExampleDelete a file
Creates and deletes the file foo.lua, checking if it exists after each.
Code
print("Making foo.lua")
local f = fs.open("foo.lua", "w")
f.close()
print("Foo exists:", fs.exists("foo.lua"))
print("Deleting foo.lua")
fs.delete("foo.lua")
print("Foo exists:", fs.exists("foo.lua"))
Output
Making foo
Foo exists: true
Deleting foo
Foo exists: false
ExampleExampleDelete a directory
Creates and deletes the directory foo, checking if it exists after each.
Code
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
print("Deleting foo")
fs.delete("foo")
print("Foo exists:", fs.exists("foo"))
Output
Making foo
Foo exists: true
Deleting foo
Foo exists: false


FunctionFunctionfs.exists
Returns true if a file or directory exists at path path, otherwise returns false.
Syntax fs.exists()
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleExampleCheck if directory exists
Creates the directory foo, checking if it exists before and after.
Code
print("Foo exists:", fs.exists("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
Output
Foo exists: false
Making foo
Foo exists: true

Fs.find Fs.getDir Fs.getDrive Fs.getFreeSpace Fs.getName Fs.getSize

FunctionFunctionfs.isDir
Returns true if a directory exists at path path. Returns false if path path is a file, or if it does not exist.
Syntax fs.isDir()
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleExampleCheck if path is directory
Creates the directory foo, checking if it is a directory before and after.
Code
print("Foo is directory:", fs.isDir("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo is directory:", fs.isDir("foo"))
Output
Foo is directory: false
Making foo
Foo is directory: true


FunctionFunctionfs.isReadOnly
Returns true if the file or directory at path path is read-only, meaning files cannot be created or changed there. Returns false if the path path is writable, or if it does not exist. /rom is an example of a read-only mount – you cannot create your own files there, or change files there.
Syntax fs.isReadOnly()
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleExampleCheck if two directories are read-only
Creates the directory foo in the root directory (/), and checks if it is read-only. Also checks if /rom/programs is read-only.
Code
print("Making foo")
fs.makeDir("foo")
print("/foo is read-only:", fs.isReadOnly("/foo"))
print("/rom/programs is read-only:", fs.isReadOnly("/rom/programs"))
Output
Making foo
/foo is read-only: false
/rom/programs is read-only: true


FunctionFunctionfs.list
Returns a table of all the files/directories in the directory at the path path.
Syntax fs.list()
Returns table
Part of CC:Tweaked (source)
API fs
ExampleExampleList files in a directory
Gets the list of files in the root directory (/) and prints them
Code
for _, name in pairs(fs.list("/")) do
    print(name)
end
Output
rom
test.lua


FunctionFunctionfs.makeDir
Creates the directory at the path path. If a file or directory at path path already exists, the function will error with File exists.
Syntax fs.makeDir()
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleExampleCreate a directory
Creates the directory foo, checking if it exists before and after.
Code
print("Foo exists:", fs.exists("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
Output
Foo exists: false
Making foo
Foo exists: true


FunctionFunctionfs.move
Moves the file at the path path to the path dest. This is effectively equivalent to renaming the file.

The function will create any directories necessary, meaning you can move a file to /foo/bar/ and it will create the directory /foo if it does not already exist.

If path dest already exists, the function will error. This means you cannot move a directory into another directory, instead you will have to manually move all the files inside of it.
Syntax fs.move()
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleExampleMove a directory
Creates the directory /foo and moves it to /bar
Code
fs.makeDir("/foo")
fs.move("/foo", "/bar")

Fs.open