Source Edit

This module implements directory handling.

Imports

paths, osdirs

Procs

  1. proc createDir(dir: Path) {.inline, ...tags: [WriteDirEffect, ReadDirEffect],
  2. raises: [OSError, IOError], forbids: [].}

Creates the directory dir.

The directory may contain several subdirectories that do not exist yet. The full path is created. If this fails, OSError is raised.

It does not fail if the directory already exists because for most usages this does not indicate an error.

See also:

Source Edit

  1. proc dirExists(dir: Path): bool {.inline, ...tags: [ReadDirEffect], sideEffect,
  2. ...raises: [], forbids: [].}

Returns true if the directory dir exists. If dir is a file, false is returned. Follows symlinks. Source Edit

  1. proc existsOrCreateDir(dir: Path): bool {.inline,
  2. ...tags: [WriteDirEffect, ReadDirEffect], raises: [OSError, IOError],
  3. forbids: [].}

Checks if a directory dir exists, and creates it otherwise.

Does not create parent directories (raises OSError if parent directories do not exist). Returns true if the directory already exists, and false otherwise.

See also:

Source Edit

  1. proc moveDir(source, dest: Path) {.inline, ...tags: [ReadIOEffect, WriteIOEffect],
  2. raises: [OSError, IOError], forbids: [].}

Moves a directory from source to dest.

Symlinks are not followed: if source contains symlinks, they themself are moved, not their target.

If this fails, OSError is raised.

See also:

Source Edit

  1. proc removeDir(dir: Path; checkDir = false) {.inline,
  2. ...tags: [WriteDirEffect, ReadDirEffect], raises: [OSError], forbids: [].}

Removes the directory dir including all subdirectories and files in dir (recursively).

If this fails, OSError is raised. This does not fail if the directory never existed in the first place, unless checkDir = true.

See also:

Source Edit

  1. proc setCurrentDir(newDir: Path) {.inline, ...tags: [], raises: [OSError],
  2. forbids: [].}

Sets the current working directory; OSError is raised if newDir cannot been set.

See also:

Source Edit

Iterators

  1. iterator walkDir(dir: Path; relative = false; checkDir = false;
  2. skipSpecial = false): tuple[kind: PathComponent, path: Path] {.
  3. ...tags: [ReadDirEffect], raises: [OSError], forbids: [].}

Walks over the directory dir and yields for each directory or file in dir. The component type and full path for each item are returned.

Walking is not recursive.

  • If relative is true (default: false) the resulting path is shortened to be relative to dir, otherwise the full path is returned.
  • If checkDir is true, OSError is raised when dir doesn’t exist.
  • If skipSpecial is true, then (besides all directories) only regular files (without special “file” objects like FIFOs, device files, etc) will be yielded on Unix.

Source Edit

  1. iterator walkDirRec(dir: Path; yieldFilter = {pcFile}; followFilter = {pcDir};
  2. relative = false; checkDir = false; skipSpecial = false): Path {.
  3. ...tags: [ReadDirEffect], raises: [OSError], forbids: [].}

Recursively walks over the directory dir and yields for each file or directory in dir.

Options relative, checkdir, skipSpecial are explained in walkDir iterator description.

Warning: Modifying the directory structure while the iterator is traversing may result in undefined behavior!

Walking is recursive. followFilter controls the behaviour of the iterator:

yieldFiltermeaning
pcFileyield real files (default)
pcLinkToFileyield symbolic links to files
pcDiryield real directories
pcLinkToDiryield symbolic links to directories
followFiltermeaning
pcDirfollow real directories (default)
pcLinkToDirfollow symbolic links to directories

See also:

Source Edit

Exports

PathComponent