dirs
nimony/lib/std/dirs.nim
This module implements operations for creating, removing, and iterating over directories.
See also:
- paths module for path handling
- syncio module for file I/O
proc tryCreateFinalDir(dir: Path): ErrorCodeTries to create the final directory in a path. In other words, it tries to create a single new directory, not a nested one. It returns the OS's error code making it easy to distinguish between "could not create" and "already exists".
proc createDir(dir: Path)Creates a new directory
dir. If the directory already exists, no error is raised. This can be used to create a nested directory structure directly.proc tryRemoveFinalDir(dir: Path): ErrorCodeTries to remove the final directory in a path. In other words, it tries to remove a single directory, not a nested one. It returns the OS's error code making it easy to distinguish between "could not remove" and "does not exist".
type DirEntry = object kind: PathComponent path: Path
type DirWalker = object stream: DirStream dir: Path status: ErrorCode
proc tryOpenDir(dir: sink Path): DirWalkerTries to open the directory
dirfor iteration over its entries.proc tryNextDir(w: var DirWalker; e: var DirEntry; skipSpecial: bool): boolAdvances
wto its next entry and stores it ine. Returns false at the end of the directory or on an error, whichwthen keeps.If
skipSpecialis true, special "file" objects (FIFOs, sockets, device files) are skipped on POSIX; otherwise they are reported aspcFile. Windows has none.proc tryCloseDir(w: var DirWalker): ErrorCodeCloses
w. A walker whosetryOpenDirfailed holds no handle, so there is nothing to close.iterator walkDir(dir: Path; relative: bool; checkDir: bool; skipSpecial: bool): tuple[kind: PathComponent, path: Path]Walks over all entries in the directory
dir.Yields tuples of
(kind, path)wherekindis one of:pcFile- regular filepcDir- directorypcLinkToFile- symbolic link to a filepcLinkToDir- symbolic link to a directory
If
relativeis true, yields relative paths (just the filename/dirname), otherwise yields full paths.If
checkDiris true, raises an error ifdirdoesn't exist or isn't a directory.Special directories "." and ".." are skipped. Special "file" objects (FIFOs, sockets, device files) are yielded as
pcFile, unlessskipSpecialis true: then they are skipped on POSIX.iterator walkDirRec(dir: Path; yieldFilter: set[PathComponent]; followFilter: set[PathComponent]; relative: bool; checkDir: bool; skipSpecial: bool): PathRecursively walks over the directory
dirand yields for each file or directory indir.Options
relative,checkDirandskipSpecialare explained in thewalkDir iterator_ description;checkDironly applies todiritself, so a subdirectory that cannot be opened is silently skipped... warning:: Modifying the directory structure while the iterator is traversing may result in undefined behavior!
Walking is recursive.
followFiltercontrols the behaviour of the iterator:===================== ============================================= yieldFilter meaning ===================== =============================================
pcFileyield real files (default)pcLinkToFileyield symbolic links to filespcDiryield real directoriespcLinkToDiryield symbolic links to directories ===================== ================================================================== ============================================= followFilter meaning ===================== =============================================
pcDirfollow real directories (default)pcLinkToDirfollow symbolic links to directories ===================== =============================================See also:
walkDir iterator_
proc removeDir(dir: Path; checkDir: bool)Removes the directory
dirincluding all subdirectories and files indir(recursively).Symlinks inside
dirare removed as links; their targets are never followed. This does not fail if the directory never existed in the first place, unlesscheckDiris true.Use
tryRemoveFinalDirto remove a single directory only if it is empty.See also:
tryRemoveFinalDir proc_removeFile proc_createDir proc_moveDir proc_
proc copyDir(source: Path; dest: Path; skipSpecial: bool)Copies a directory from
sourcetodest.On non-Windows OSes, symlinks are copied as symlinks. On Windows, symlinks are skipped.
If
skipSpecialis true, then (besides all directories) only regular files (without special "file" objects like FIFOs, device files, etc) will be copied on POSIX.On Windows this proc copies the attributes from
sourceintodest. On other platforms created files and directories get the default permissions of a newly created file/directory for the user. UsecopyDirWithPermissions proc_ to preserve them recursively.See also:
copyDirWithPermissions proc_- copyFile proc
removeDir proc_createDir proc_moveDir proc_
proc copyDirWithPermissions(source: Path; dest: Path; ignorePermissionErrors: bool; skipSpecial: bool)Copies a directory from
sourcetodestpreserving file permissions.On non-Windows OSes, symlinks are copied as symlinks. On Windows, symlinks are skipped.
skipSpecialis as forcopyDir proc_.On Windows this is just
copyDir proc_, which already copies the attributes. On other systems the permissions of each file and directory are applied after it has been copied, which does not happen atomically. IfignorePermissionErrorsis true (default), errors while reading or setting them are ignored; otherwise they are raised.See also:
copyDir proc_- copyFileWithPermissions proc
removeDir proc_moveDir proc_
proc moveDir(source: Path; dest: Path)Moves a directory from
sourcetodest.Symlinks are not followed: if
sourcecontains symlinks, they themselves are moved, not their targets. As withrename, an existingdestmust be an empty directory, which is replaced.Within one filesystem the move is a single
rename. Across filesystems (EXDEVon POSIX, an access error on Windows) it falls back tocopyDir procfollowed byremoveDir proconsource, as Nim does.See also:
- moveFile proc
copyDir proc_createDir proc_removeDir proc_
proc getCurrentDir(): PathReturns the current working directory as a
Path.Raises an error if unable to retrieve the current directory.
See also:
setCurrentDir proc_
proc setCurrentDir(dir: Path)Sets the current working directory to
dir.Raises errors if the directory doesn't exist or lacks permissions.
See also:
getCurrentDir proc_