From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA02028 for caml-red; Thu, 26 Oct 2000 20:39:40 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA00623 for ; Thu, 26 Oct 2000 18:42:09 +0200 (MET DST) Received: from pluton.ac-grenoble.fr (pluton.ac-grenoble.fr [193.54.149.10]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id e9QGfcb20260 for ; Thu, 26 Oct 2000 18:41:38 +0200 (MET DST) Received: from haguenauer.dijon.fr (root@saturn66.ac-grenoble.fr [195.221.235.36]) by pluton.ac-grenoble.fr (8.10.1/8.10.1) with ESMTP id e9QGe3M17825 for ; Thu, 26 Oct 2000 18:40:04 +0200 (MET DST) Received: from localhost (localhost [[UNIX: localhost]]) by haguenauer.dijon.fr (8.10.2/8.10.2) id e9QIBYT23709 for caml-list@inria.fr; Thu, 26 Oct 2000 18:11:34 GMT From: Michel Quercia Reply-To: quercia@cal.enst.fr To: "caml-list" Subject: Re: Seeking directory traversal example Date: Thu, 26 Oct 2000 18:06:57 +0000 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain References: <200010260531.BAA93211@sanson.reyes.somos.net> In-Reply-To: <200010260531.BAA93211@sanson.reyes.somos.net> MIME-Version: 1.0 Message-Id: <00102618113402.23346@haguenauer> Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr 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 = 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