11.10. Directory Handling Functions

11.10.1. Dir

A Directory object, as well as associated functions.

11.10.1.1. Dir.make(name)

Creates a directory.

The created directory is set for permission mode 0755 (octal), meaning it is read+write+execute by owner, but only read+execute by group members and others.

If the directory was created successfully, a boolean true is returned. If the directory cannot be made because it already exists, false is returned. If the directory cannot be made because an error occurred, nil is returned.

Since: 1.11.3

Arguments
name
The name of the directory, possibly including path.
Returns

Boolean true on success, false if the directory already exists, nil on error.

11.10.1.2. Dir.exists(name)

Returns true if the given directory name exists.

If the directory exists, a boolean true is returned. If the path is a file instead, false is returned. If the path does not exist or an error occurred, nil is returned.

Since: 1.11.3

Arguments
name
The name of the directory, possibly including path.
Returns

Boolean true if the directory exists, false if it’s a file, nil on error or not-exist.

11.10.1.3. Dir.remove(name)

Removes an empty directory.

If the directory was removed successfully, a boolean true is returned. If the directory cannot be removed because it does not exist, false is returned. If the directory cannot be removed because an error occurred, nil is returned.

This function only removes empty directories. To remove a directory regardless, use Dir.remove_all().

Since: 1.11.3

Arguments
name
The name of the directory, possibly including path.
Returns

Boolean true on success, false if does not exist, nil on error.

11.10.1.4. Dir.remove_all(name)

Removes an empty or non-empty directory.

If the directory was removed successfully, a boolean true is returned. If the directory cannot be removed because it does not exist, false is returned. If the directory cannot be removed because an error occurred, nil is returned.

Since: 1.11.3

Arguments
name
The name of the directory, possibly including path.
Returns

Boolean true on success, false if does not exist, nil on error.

11.10.1.5. Dir.open(pathname, [extension])

Opens a directory and returns a Dir object representing the files in the directory.

11.10.2. Example

    -- Print the contents of a directory
    for filename in Dir.open('/path/to/dir') do
            print(filename)
    end
Arguments
pathname
The pathname of the directory.
extension (optional)
If given, only files with this extension will be returned.
Returns

The Dir object.

11.10.2.1. Dir.personal_config_path([filename])

Gets the personal configuration directory path, with filename if supplied.

Since: 1.11.3

Arguments
filename (optional)
A filename.
Returns

The full pathname for a file in the personal configuration directory.

11.10.2.2. Dir.global_config_path([filename])

Gets the global configuration directory path, with filename if supplied.

Since: 1.11.3

Arguments
filename (optional)
A filename
Returns

The full pathname for a file in Wireshark’s configuration directory.

11.10.2.3. Dir.personal_plugins_path()

Gets the personal plugins directory path.

Since: 1.11.3

Returns

The pathname of the personal plugins directory.

11.10.2.4. Dir.global_plugins_path()

Gets the global plugins directory path.

Since: 1.11.3

Returns

The pathname of the global plugins directory.

11.10.2.5. dir:__call()

Gets the next file or subdirectory within the directory, or nil when done.

11.10.3. Example

    -- Open a directory and print the name of the first file or subdirectory
    local dir = Dir.open('/path/to/dir')
    local first = dir()
    print(tostring(file))

11.10.3.1. dir:close()

Closes the directory. Called automatically during garbage collection of a Dir object.