[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Michel Quercia <quercia@c...> |
| Subject: | Re: Seeking directory traversal example |
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