caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Seeking directory traversal example
@ 2000-10-26  5:34 Francisco Reyes
  2000-10-26 18:06 ` Michel Quercia
  0 siblings, 1 reply; 2+ messages in thread
From: Francisco Reyes @ 2000-10-26  5:34 UTC (permalink / raw)
  To: Caml List

Anyone has sample code I could use for Ocaml to traverse a
directory?

Basically I am trying to create a program to clean old files.
This program will accept two parameters date and days. Any files
older than "days" will be deleted.


francisco
Moderator of the Corporate BSD list
http://www.egroups.com/group/BSD_Corporate




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Seeking directory traversal example
  2000-10-26  5:34 Seeking directory traversal example Francisco Reyes
@ 2000-10-26 18:06 ` Michel Quercia
  0 siblings, 0 replies; 2+ messages in thread
From: Michel Quercia @ 2000-10-26 18:06 UTC (permalink / raw)
  To: caml-list

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-10-26 18:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-26  5:34 Seeking directory traversal example Francisco Reyes
2000-10-26 18:06 ` Michel Quercia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).