Module fs

Filesystem utilities module

Functions

directoryExists (path) Check if a directory exists
fileExists (path, filename) Check if a file exists
ensureDirectory (path) Ensure the given path is a directory
shortenPath (path, opts) Get a shorter version of a path


Functions

directoryExists (path)
Check if a directory exists

Parameters:

  • path string The path of the directory to check

Returns:

    boolean # true if path is a directory, false otherwise (non existent or file)

Raises:

error if path is not a string

Usage:

    if directoryExists("/tmp/mydir") then
        print("My dir exists!")
    else
        print("My dir does not exists :(
    end
fileExists (path, filename)
Check if a file exists

Parameters:

  • path string The path that contains the file to find
  • filename string|nil The name of the file to check if it exists.
    If this argument is nil, the filename should be contained inside path

Returns:

    boolean # true if file exists, false otherwise (non existent or directory)

Raises:

error if path is not a string
error if filename is not nil and not a string

Usage:

    local path = "/tmp/mydir/"
    local filename = "myfile.txt"
    if fileExists(path, filename) then
        print("My file exists!")
    else
        print("My file does not exists :(")
    end
     -- OR
    local filepath = "/tmp/mydir/myfile.txt"
    if fileExists(filepath) then
        print("My file exists!")
    else
        print("My file does not exists :(")
    end
ensureDirectory (path)
Ensure the given path is a directory

Parameters:

  • path string The path of the directory to check

Returns:

    boolean # true if the path is a directory, false if something went wrong (e.g. cannot create directory)

Raises:

error if path is not a string

Usage:

    local path = "/tmp/mydir"
    if directoryExists(path) then -- No
        print("Yes")
    end
    if ensureDirectory(path) then -- No
        print("An error occured while ensuring dir exists")
    end
    if directoryExists(path) then -- Yes
        print("Yes")
    end
    if ensureDirectory(path) then -- No
        print("An error occured while ensuring dir exists")
    end
    if directoryExists(path) then -- Yes
        print("Yes")
    end
shortenPath (path, opts)
Get a shorter version of a path

Parameters:

  • path string The path to shorten
  • opts

    ? table Optional table of parameters to custom the returned value

       opts.len: The maximum length of a path component, shortened components will be of size len. Default: 1<br/>
       opts.tail: The number of tail components to keep unshortened. Default: 1<br/>
       opts.maxComponents: The maximum number of components to keep. If the path is composed of too many components, the head component will be kept and everything following the head will be replaced by "…" to match the component number. Cannot be under 2. 0 means no limit. Default: 0<br/>
       opts.relative: If the path should be made relative to the current working directory. Default: true
       opts.maxLength: The maximum length (number of characters) of the resulted string. 0 means no limit. Default: 0
    

Returns:

    string # The path shorten as string

Raises:

error if path is not a string
error if opts is not nil and not a table

Usage:

    local path = "/tmp/adir/bdir/cdir/myfile"
    print(shortenPath(path)) -- "/t/a/b/c/myfile"
    print(shortenPath(path, { len = 3 })) -- "/tmp/adi/bdi/cdi/myfile"
    print(shortenPath(path, { tail = 2 })) -- "/t/a/b/cdir/myfile"
    print(shortenPath(path, { len = 2, maxComponents = 3 })) -- "/tm/…/cd/myfile"
    print(shortenPath(path, { len = 2, maxLength = 13 })) -- "/tm/…/myfile"
generated by LDoc 1.5.0 Last updated 2024-08-17 23:38:16