fs.attributes

From ComputerCraft
Jump to navigation Jump to search

fs.attributes
Function
Syntax
fs.attributes()
Returns table attributes
API fs
Source CC:Tweaked (source)

Gets information about the file or directory specified by path. It returns a table of attributes containing the date and time the file was created and modified, the size of the file in bytes, and whether or not the path is a directory. If the file does not exist, you will receive a No such file error.

The times returned from created and modification can be used with the os.date function to format them to a human-readable time.
The times may depend on the server's underlying filesystem. While their values are in milliseconds, they may be truncated to the nearest second.
Attributes returned by fs.attributes
Attribute Type Example Function Description
created number 1587778697000 N/A The date and time the file or directory was created, as a Unix time in milliseconds.
modification number 1587781449000 N/A The date and time the file or directory was last modified, as a Unix time in milliseconds.
isDir boolean false fs.isDir Whether or not this path is a directory.
size number 16 fs.getSize The size of this file, in bytes. Returns 0 for directories.
ExampleExampleProgram to print information about a path
Prints information about path passed in as an argument. The program can be used from the CraftOS shell with the command attributes [path], for example: attributes example.txt. The times are formatted using os.date.
Code
-- Get the path from the command
local path = ...
if not path then error("Usage: attributes [path]") end

-- Get the file attributes
local attr = fs.attributes(path)

if attr.isDir then
  print(path, "is a directory.")
else
  print(path, "is a file.")
  print("File size:", attr.size, "bytes")
end

-- Format the times in a human-friendly way. See: os.date()
-- Note: os.date() expects seconds, but we have milliseconds, so we must divide
--       the times by 1000 to get them in seconds.
print("Created:", os.date("%c", attr.created / 1000))
print("Modified:", os.date("%c", attr.modification / 1000))
Output Fs.attributes example.png