FS API

Functionfs.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(
  • pathA : string
  • pathB : string
)
Returns string
Part of CC:Tweaked (source)
API fs
ExampleCreating a path to a directory within the ROM directory
Prints the combination of the paths /rom and /dir/test.
Code
<nowiki>
print(fs.combine("/rom", "/dir/test"))
    </nowiki>
Output rom/dir/test

Fs.complete

Functionfs.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(
  • path : string
  • dest : string
)
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleCopy a file to a directory
Copies the edit program (/rom/programs/edit.lua) to the root directory (/).
Code
<nowiki>
fs.copy("/rom/programs/edit.lua", "/")
    </nowiki>
Output A copy of the edit program will now be located at /edit.lua.
ExampleCopy a file with a new name
Copies the clear program (/rom/programs/clear.lua) to /cls.lua.
Code
<nowiki>
fs.copy("/rom/programs/clear.lua", "/cls.lua")
    </nowiki>
Output A copy of the clear program will now be located at /cls.lua
Functionfs.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: This function is irreversible! Make sure you enter the arguments carefully so that you do not delete anything important.
Syntax fs.delete(
  • path : string
)
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleDelete a file
Creates and deletes the file foo.lua, checking if it exists after each.
Code
<nowiki>
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"))
    </nowiki>
Output
<nowiki>
Making foo
Foo exists: true
Deleting foo
Foo exists: false
</nowiki>
ExampleDelete a directory
Creates and deletes the directory foo, checking if it exists after each.
Code
<nowiki>
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
print("Deleting foo")
fs.delete("foo")
print("Foo exists:", fs.exists("foo"))
    </nowiki>
Output
<nowiki>
Making foo
Foo exists: true
Deleting foo
Foo exists: false
</nowiki>
Functionfs.exists
Returns true if a file or directory exists at path path, otherwise returns false.
Syntax fs.exists(
  • path : string
)
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleCheck if directory exists
Creates the directory foo, checking if it exists before and after.
Code
<nowiki>
print("Foo exists:", fs.exists("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
    </nowiki>
Output
<nowiki>
Foo exists: false
Making foo
Foo exists: true
</nowiki>
Functionfs.find
Returns a table containing all files and folders that match a wildcard expansion search.
Syntax fs.find(
  • wildcard : string
)
Returns table
Part of CC:Tweaked (source)
API fs
ExampleList all Lua scripts contained within a directory
Prints all files that end with .lua within /rom.
Code
<nowiki>
for _, path in ipairs(fs.find("/rom/*.lua")) do
    print(path)
end</nowiki>
Output
<nowiki>rom/startup.lua</nowiki>
Functionfs.getDir
Returns the directory part of a file.
Syntax fs.getDir(
  • path : string
)
Returns string
Part of CC:Tweaked (source)
API fs
ExampleGet the directory
Get the directory of the path "/rom/programs/shell.lua" and print it.
Code
<nowiki>
print(fs.getDir("/rom/programs/shell.lua"))
    </nowiki>
Output
<nowiki>
/rom/programs
</nowiki>
Functionfs.getDrive
Returns the type of the medium, where the data is stored. Returns nil, if the path doesn't exists.
Syntax fs.getDrive(
  • path : string
)
Returns string medium
Part of CC:Tweaked (source)
API fs
ExampleGet the medium
Get the medium of the "/rom/programs/shell.lua" and print it.
Code
<nowiki>
print(fs.getDrive("/rom/programs/shell.lua"))
    </nowiki>
Output
<nowiki>
rom
</nowiki>
Functionfs.getFreeSpace
Returns the free space in a directory in bytes.
Syntax fs.getFreeSpace(
  • path : string
)
Returns number space
Part of CC:Tweaked (source)
API fs
ExampleGet free space
Get the free space of the computer and print it.
Code
<nowiki>
print(fs.getFreeSpace("/"))
    </nowiki>
Output
<nowiki>
100000
</nowiki>
Functionfs.getName
Returns the filename part of a path.
Syntax fs.getName(
  • path : string
)
Returns string
Part of CC:Tweaked (source)
API fs
ExampleGet the filename
Get the filename of the path "/rom/programs/shell.lua" and print it.
Code
<nowiki>
print(fs.getName("/rom/programs/shell.lua"))
    </nowiki>
Output
<nowiki>
shell.lua
</nowiki>
Functionfs.getSize
Returns the size of a file in Bytes. Returns 0 if path is a directory.
Syntax fs.getSize(
  • path : string
)
Returns number
Part of CC:Tweaked (source)
API fs
ExamplePrint the size of "edit"
Print the Size of "/rom/programs/edit.lua" in Bytes.
Code
<nowiki>
print(fs.getSize("/rom/programs/edit.lua"))
    </nowiki>
Output
22975
Functionfs.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(
  • path : string
)
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleCheck if path is directory
Creates the directory foo, checking if it is a directory before and after.
Code
<nowiki>
print("Foo is directory:", fs.isDir("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo is directory:", fs.isDir("foo"))
    </nowiki>
Output
<nowiki>
Foo is directory: false
Making foo
Foo is directory: true
</nowiki>
Functionfs.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(
  • path : string
)
Returns boolean
Part of CC:Tweaked (source)
API fs
ExampleCheck 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
<nowiki>
print("Making foo")
fs.makeDir("foo")
print("/foo is read-only:", fs.isReadOnly("/foo"))
print("/rom/programs is read-only:", fs.isReadOnly("/rom/programs"))
    </nowiki>
Output
<nowiki>
Making foo
/foo is read-only: false
/rom/programs is read-only: true
</nowiki>
Functionfs.list
Returns a table of all the files/directories in the directory at the path path.
Syntax fs.list(
  • path : string
)
Returns table
Part of CC:Tweaked (source)
API fs
ExampleList files in a directory
Gets the list of files in the root directory (/) and prints them
Code
<nowiki>
for _, name in pairs(fs.list("/")) do
    print(name)
end
    </nowiki>
Output
<nowiki>
rom
test.lua</nowiki>
Functionfs.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(
  • path : string
)
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleCreate a directory
Creates the directory foo, checking if it exists before and after.
Code
<nowiki>
print("Foo exists:", fs.exists("foo"))
print("Making foo")
fs.makeDir("foo")
print("Foo exists:", fs.exists("foo"))
    </nowiki>
Output
<nowiki>
Foo exists: false
Making foo
Foo exists: true
</nowiki>
Functionfs.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(
  • path : string
  • dest : string
)
Returns nil
Part of CC:Tweaked (source)
API fs
ExampleMove a directory
Creates the directory /foo and moves it to /bar
Code
<nowiki>
fs.makeDir("/foo")
fs.move("/foo", "/bar")
    </nowiki>
Functionfs.open
Opens a file for reading, writing, or appending. Writing to a file that does not exist creates the file. Mode Must be either w (writing), a (appending), or r (reading)
Syntax fs.open(
  • Path : string
  • Mode : char
)
Returns Input Handler (w/a) Output Handler (r)
Part of CC:Tweaked (source)
API fs
ExampleRead A File
Read the contents of a file
Code
<nowiki>
local handler = fs.open("test.txt", "r")
print(handler.readAll())
handler.close()
    </nowiki>
Output
true
ExampleAppend to A File
Append text to the contents of a file
Code
<nowiki>
local handler = fs.open("test.txt", "a")
handler.write(World!”)
handler.close()
    </nowiki>
Output
true
ExampleWrite to A File
Override the contents of a file
Code
<nowiki>
local handler = fs.open("test.txt", "w")
handler.write("Hello World!")
handler.close()
    </nowiki>
Output
true
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.