13.12. Directory Handling Functions
Prev | Chapter 13. Wireshark’s Lua API Reference Manual | Next

## 13.12. Directory Handling Functions

### 13.12.1. Dir

A Directory object, as well as associated functions.

#### 13.12.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.

##### Arguments

name
  The name of the directory, possibly including path.

##### Returns

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

#### 13.12.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.

##### 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.

#### 13.12.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()`.

##### Arguments

name
  The name of the directory, possibly including path.

##### Returns

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

#### 13.12.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.

##### Arguments

name
  The name of the directory, possibly including path.

##### Returns

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

#### 13.12.1.5. Dir.open(pathname, [extension])

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

### 13.12.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.

#### 13.12.2.1. Dir.personal_config_path([filename])

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

##### Arguments

filename (optional)
  A filename.

##### Returns

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

#### 13.12.2.2. Dir.global_config_path([filename])

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

##### Arguments

filename (optional)
  A filename

##### Returns

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

#### 13.12.2.3. Dir.personal_plugins_path()

Gets the personal plugins directory path.

##### Returns

The pathname of the personal plugins directory.

#### 13.12.2.4. Dir.global_plugins_path()

Gets the global plugins directory path.

##### Returns

The pathname of the global plugins directory.

#### 13.12.2.5. dir:__call()

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

### 13.12.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))

#### 13.12.3.1. dir:close()

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

#### 13.12.3.2. dir:__tostring()

Returns a short label of the form `Dir: path=<path>` (with `(closed)` appended once `Dir:close()` has run). `path` is the directory Dir.open() resolved to, so symlinks and relative paths show as their canonical form.

##### Returns

The string.

Prev | Up | Next
13.11. Custom File Format Reading And Writing | Home | 13.13. Handling 64-bit Integers