List Info

Thread: "ocaml_beginners"::[] Walking through directory a portable way ?




"ocaml_beginners"::[] Walking through directory a portable way ?
country flaguser name
United States
2007-02-24 04:34:48

Hi !

Please what library can we use to move in a directory tree ?
I've noticed Unix module but maybe is it possible to use another lib
in order the program can work both on unices and Windoze ?

If you know an OCaml "tree" program, that's shows the directory
structures. I would be very glad to study it's sources.

Thanks

Fabrice

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: Walking through directory a portable way ?
country flaguser name
United States
2007-02-24 04:40:35

About the "tree" source : if it only works on UNIX, I remain
interested in it.

Regards

Fabrice

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Walking through directory a portable way ?
country flaguser name
United Kingdom
2007-02-24 04:46:14

On Sat, Feb 24, 2007 at 10:34:48AM -0000, fabrice.marchant wrote:
> Hi !
>
> Please what library can we use to move in a directory tree ?
> I've noticed Unix module but maybe is it possible to use another lib
> in order the program can work both on unices and Windoze ?
>
> If you know an OCaml "tree" program, that's shows the directory
> structures. I would be very glad to study it's sources.

I would start with Sys.readdir, which returns a list of names in the
directory.

Prepend the directory path on each using Filename.concat.

Then test for subdirectories and recurse using Unix.stat (this should
work on Windoze too), checking st_kind = S_DIR.

There is also library support in various places, eg:
Annexlib: http://raevnos.pennmush.org/code/annexlib/doc/Find.html
Cash: http://pauillac.inria.fr/cash/latest/doc/Cash.html
FileUtils: http://sylvain.le-gall.net/ocaml-fileutils.html
(apologies if I've missed any more).

Rich.

--
Richard Jones
Red Hat UK Limited

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Walking through directory a portable way ?
country flaguser name
France
2007-02-25 04:38:23

Thanks to Richard and Rémy !

Richard : you gave me all elements to access directory informations.
Rémy, it was useful you teached me that several functions of Unix module could work on Windows.

Thanks for the links too.

Now I can read directories and identify subdirs.
( I'm not sure this "compose" is the best way to write things. If not, please, thanks to improve ! )
(****************************************************************************)
let compose f g x = f (g x)

let dir_content = Sys.readdir "."

let show_dir f = f ^ if (Unix.stat f).Unix.st_kind = Unix.S_DIR then
" is a dir."
else
""

let () = Array.iter (compose print_endline
(compose show_dir
(Filename.concat (Sys.getcwd()))))
dir_content
(****************************************************************************)

I've all that is necessary to do what I wanted :
writing a program that'll inspect the directories and report biggest redundant branches : a kind of "fdupes".

Best regards

Fabrices

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Walking through directory a portable way ?
country flaguser name
France
2007-02-25 08:59:43

Michaël wrote:
> you can write the compose thing as operator like that (like a pipe):
>
> let ( |> ) a b = b a;;
...

Beautiful !

Thanks a lot ! The more I learn about OCaml, the more I discover that it is pleasant and powerful.

( Who will have the kindness to put an announce on C++ or Java lists to explain it does exists a much better language ? 3 months ago I was an old C++ programmer : I'm happy to forget this. )

Fabrice

__._,_.___
.

__,_._,___
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )