Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Michel Quercia <quercia@cal.enst.fr>
To: "caml-list" <caml-list@inria.fr>
Subject: Re: Seeking directory traversal example
Date: Thu, 26 Oct 2000 18:06:57 +0000	[thread overview]
Message-ID: <00102618113402.23346@haguenauer> (raw)
In-Reply-To: <200010260531.BAA93211@sanson.reyes.somos.net>

Le Thu, 26 Oct 2000, vous avez écrit :
> Anyone has sample code I could use for Ocaml to traverse a
> directory?

Here is a small example

(* body = what to do with a filename *)
(* root = root of the directory tree, with a trailing slash *)

let rec dir_iter body root =
  let h = Unix.opendir root in
  try while true do
    let f = Unix.readdir h in
    if (f <> ".") && (f <> "..") then begin
      let fn = root ^ f in
      body fn;
      if (Unix.lstat fn).Unix.st_kind = Unix.S_DIR then dir_iter body (fn ^ "/")
    end
  done
  with End_of_file -> Unix.closedir h
;;

val dir_iter : (string -> 'a) -> string -> unit = <fun>

Note that you may need to catch more exceptions than just Eno_of_file, for
example trying to browse a directory for which you don't have read permission
will trigger the exception : Unix.Unix_error (Unix.EACCES, "opendir", "...")
-- 
Michel Quercia
57 rue abbé Grégoire, 38000 Grenoble
http://pauillac.inria.fr/~quercia
mailto:quercia@cal.enst.fr



      reply	other threads:[~2000-10-26 18:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-10-26  5:34 Francisco Reyes
2000-10-26 18:06 ` Michel Quercia [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=00102618113402.23346@haguenauer \
    --to=quercia@cal.enst.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox