1. Overview
  2. Docs

This modules provides functions to create directories, read, iter or remove directories, recursively or not.

type t

Type of a filename. It is string in FileString and FileGen.t in FileGen.

exception NotADirectory of t

This exception is raised when one of the following functions is called with a non-directory argument

val make_dir : ?mode:int -> ?p:bool -> t -> unit

make_dir ?mode ?p filename creates a directory filename, if it does not already exist. It fails with NotADirectory if the file already exists, but is not a directory. The mode argument is the Unix permissions (0o755 by default). The p argument controls whether parents directories should be created as well, if they don't exist, instead of failing.

val remove_dir : ?all:bool -> ?glob:string -> t -> unit

remove_dir ?all filename removes directory filename, or complains the NotADirectory if it does not exist. The all argument controls whether the function should recursively remove all files and sub-directories included as well. If glob is specified, it is called to select files to remove, and the directories are not deleted even if all is true.

val select : ?deep:bool -> ?dft:[ `After | `Before ] -> ?glob:string -> ?filter:(bool -> string -> string -> bool) -> ?follow_links:bool -> ?error:(exn -> string -> t -> unit) -> unit -> t FileSelector.t

select ?deep ?dft ?glob ?filter ?follow_links ?error () creates a selctor to customize a file iterator.

The deep and dft arguments controls whether function should recurse in sub-directories. If deep is true, and ~dft is not specified, the files are listed in breadth-first mode (a,b,a/x,b/x,a/x/y for example). If ~dft is `Before, the files are listed in depth-first mode, and the ancestors are before their children. If ~dft is `After, the are after their children.

The glob argument can be used to filter the basenames of files with a regular expression.

The filter argument is called as filter is_dir basename path where is_dir is set when checking whether to enter or not into a sub-directory, basename is the basename of the file and path is the path starting with a '/', yet relative to the initial directory. filter is called on every file with is_dir false to decide whether it should be added or not, and only on sub-directories with is_dir true to decide whether to enter or not if deep is true.

The follow_links argument is used to decide if a link to directory should be followed (when deep is also set).

The error argument is called when an error occurs, with error exn path filename.

val read_dir : ?select:t FileSelector.t -> t -> t array

read_dir ?select filename returns the files contained in the directory filename.

In a directory, files are sorted in lexicographical order of their names.

val read_dir_to_list : ?select:t FileSelector.t -> t -> t list

Same as read_dir, but returns a list instead of an array

val iter_dir : ?select:t FileSelector.t -> (basename:string -> localpath:string -> file:t -> unit) -> t -> unit

Same as read_dir, but calls a function on every file and directory with the basename, the relative path (yet, starting with a '/') and the filename (i.e. the directory name concatenated with the relative path): f basename path file. It is not equivalent to using read_dir and then itering on the result, as iter_dir the function is called during the traversal, not after.

val iterator : ?select:t FileSelector.t -> t -> unit -> (string * t) option

iterator ?select dir creates an iterator on directory dir. The iterator is a function that returns None when finished, or Some (path, filename) with the next file to iter on.

val mkdir : t -> int -> unit

mkdir filename mode simply creates the directory filename with permissions mode.

val readdir : t -> string array

readdir filename returns the files contained in directory filename as an array of strings. The strings are sorted in lexicographical order.

val rmdir : t -> unit

rmdir filename removes directory filename, or fails if it does not exist or is not a directory.