From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id A39247ED34 for ; Tue, 3 Jul 2012 14:15:07 +0200 (CEST) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of daniel.buenzli@erratique.ch) identity=pra; client-ip=74.55.86.74; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="daniel.buenzli@erratique.ch"; x-sender="daniel.buenzli@erratique.ch"; x-conformance=sidf_compatible Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of daniel.buenzli@erratique.ch) identity=mailfrom; client-ip=74.55.86.74; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="daniel.buenzli@erratique.ch"; x-sender="daniel.buenzli@erratique.ch"; x-conformance=sidf_compatible Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@smtp.webfaction.com) identity=helo; client-ip=74.55.86.74; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="daniel.buenzli@erratique.ch"; x-sender="postmaster@smtp.webfaction.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnYCADbh8k9KN1ZKnGdsb2JhbABFhVqxMgEBAQEBCAsJCRQnghkBBSMEUhALGgImAgJHEAYbiAMEB6gIkzWBII8gMmADlkeEMRONDg X-IronPort-AV: E=Sophos;i="4.77,515,1336341600"; d="scan'208";a="165415671" Received: from mail6.webfaction.com (HELO smtp.webfaction.com) ([74.55.86.74]) by mail1-smtp-roc.national.inria.fr with ESMTP; 03 Jul 2012 14:15:06 +0200 Received: from [192.168.0.102] (5-232.197-178.cust.bluewin.ch [178.197.232.5]) by smtp.webfaction.com (Postfix) with ESMTP id 7591D2113EA8; Tue, 3 Jul 2012 07:15:03 -0500 (CDT) Date: Tue, 3 Jul 2012 14:14:59 +0200 From: =?utf-8?Q?Daniel_B=C3=BCnzli?= To: Ivan Cc: caml-list@inria.fr Message-ID: In-Reply-To: <271211341286221@web29f.yandex.ru> References: <271211341286221@web29f.yandex.ru> X-Mailer: sparrow 1.6.1 (build 1081.52) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: Re: [Caml-list] howto: recursively iterate over filesystem Hello, The book on Unix system programming in OCaml shows how to write a `find` function. http://ocamlunix.forge.ocamlcore.org/files.html#htoc11 But if you don't need tight control over the process and don't want to link against the Unix module, just roll your own with the functions in Sys. That's what I usually use (note that it does follow symlinks). val fold_files_rec : string list -> ('a -> string -> 'a) -> 'a -> 'a (** [fold_files_rec dirs f acc] lists the files in [dirs], recursively in depth first order and folds the function [f] over the file names. *) let fold_files_rec dirs f acc = let readdir d = try Array.to_list (Sys.readdir d) with Sys_error _ -> [] in let is_dir d = try Sys.is_directory d with Sys_error _ -> false in let rec loop f acc = function | (d :: ds) :: up -> let files = List.rev (List.rev_map (Filename.concat d) (readdir d)) in let dirs, files = List.partition is_dir files in let acc = List.fold_left f acc files in loop f acc (dirs :: ds :: up) | [] :: [] -> acc | [] :: up -> loop f acc up | _ -> assert false in loop f acc (dirs :: []) Best, Daniel