Functions - file

copy
Copy the file/directory in the old path to the new path.
createDir
Creates a new directory with the specified file name.
createFile
Creates a file in the specified file path.
exists
Reports whether the file or directory exists in the given the path.
getCurrentDirectory
Returns the current working directory.
getFileInfo
Returns the metadata information of the file specified in the file path.
readDir
Reads the directory and returns a list of files and directories inside the specified directory.
remove
Removes the specified file or directory.
rename
Renames(Moves) the old path with the new path.
tempDir
Returns the default directory to use for temporary files.

copy

(string sourcePath, string destinationPath, boolean replaceExisting)

returns Error?

Copy the file/directory in the old path to the new path. If a file already exists in the new path, this replaces that file.

 file:Error? results = file:copy("/A/B/C", "/A/B/D", true);

Parameters

  • sourcePath string
  • String value of the old file path

  • destinationPath string
  • String value of the new file path

  • replaceExisting boolean (default false)
  • Flag to replace if the file already exists in the destination path

  • Return Type

    (Error?)
  • An file:Error if failed to rename

createDir

(string dir, boolean parentDirs)

returns string | Error

Creates a new directory with the specified file name. If the parentDirs flag is true, it creates a directory in the specified path with any necessary parents.

 string | error results = file:createDir("foo/bar");

Parameters

  • dir string
  • Directory name

  • parentDirs boolean (default false)
  • Indicates whether the createDir should create non-existing parent directories

  • Return Type

    (string | Error)
  • Absolute path value of the created directory or else an file:Error if failed

createFile

(string path)

returns string | Error

Creates a file in the specified file path. Truncates if the file already exists in the given path.

 string | error results = file:createFile("bar.txt");

Parameters

  • path string
  • String value of the file path

  • Return Type

    (string | Error)
  • Absolute path value of the created file or else an file:Error if failed

exists

(string path)

returns boolean

Reports whether the file or directory exists in the given the path.

 boolean result = file:exists("foo/bar.txt");

Parameters

  • path string
  • String value of the file path

  • Return Type

    (boolean)
  • True if the path is absolute or else false

getCurrentDirectory

()

returns string

Returns the current working directory.

 string dirPath = file:getCurrentDirectory();

  • Return Type

    (string)
  • Current working directory or else an empty string if the current working directory cannot be determined

getFileInfo

(string path)

returns FileInfo | Error

Returns the metadata information of the file specified in the file path.

 file:FileInfo | error result = file:getFileInfo("foo/bar.txt");

Parameters

  • path string
  • String value of the file path.

  • Return Type

    (FileInfo | Error)
  • The FileInfo instance with the file metadata or else an file:Error

readDir

(string path, int maxDepth)

returns FileInfo[] | Error

Reads the directory and returns a list of files and directories inside the specified directory.

 file:FileInfo[] | error results = file:readDir("foo/bar");

Parameters

  • path string
  • String value of the directory path.

  • maxDepth int (default -1)
  • The maximum number of directory levels to visit. -1 to indicate that all levels should be visited

  • Return Type

    (FileInfo[] | Error)
  • The FileInfo array or else an file:Error if there is an error while changing the mode.

remove

(string path, boolean recursive)

returns Error?

Removes the specified file or directory. If the recursive flag is true, it removes the path and any children it contains.

 file:Error? results = file:remove("foo/bar.txt");

Parameters

  • path string
  • String value of the file/directory path

  • recursive boolean (default false)
  • Indicates whether the remove should recursively remove all the files inside the given directory

  • Return Type

    (Error?)
  • An file:Error if failed to remove

rename

(string oldPath, string newPath)

returns Error?

Renames(Moves) the old path with the new path. If the new path already exists and it is not a directory, this replaces the file.

 file:error? results = file:rename("/A/B/C", "/A/B/D");

Parameters

  • oldPath string
  • String value of the old file path

  • newPath string
  • String value of the new file path

  • Return Type

    (Error?)
  • An file:Error if failed to rename

tempDir

()

returns string

Returns the default directory to use for temporary files.

 string results = file:tempDir();

  • Return Type

    (string)
  • Temporary directory location