Skip to content

Instantly share code, notes, and snippets.

@lindig
Last active August 5, 2021 10:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lindig/be55f453026c65e761f4e7012f8ab9b5 to your computer and use it in GitHub Desktop.
Save lindig/be55f453026c65e761f4e7012f8ab9b5 to your computer and use it in GitHub Desktop.
OCaml - list recursively regular files in a directory
(** [dir_is_empty dir] is true, if [dir] contains no files except
* "." and ".."
*)
let dir_is_empty dir =
Array.length (Sys.readdir dir) = 0
(** [dir_contents] returns the paths of all regular files that are
* contained in [dir]. Each file is a path starting with [dir].
*)
let dir_contents dir =
let rec loop result = function
| f::fs when Sys.is_directory f ->
Sys.readdir f
|> Array.to_list
|> List.map (Filename.concat f)
|> List.append fs
|> loop result
| f::fs -> loop (f::result) fs
| [] -> result
in
loop [] [dir]
@gleventhal
Copy link

I believe there is Sys.ls_dir which returns list, at least Janestreet Core has this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment