Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Unix-Filekind => codexample with questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-21 (21:33)
From: John Prevost <visigoth@c...>
Subject: Re: [Caml-list] Unix-Filekind => codexample with questions
>>>>> "ob" == Oliver Bandel <> writes:

    ob> Hello, I need a function, which tells me if a file is a
    ob> regular file or not.

    ob> Please look at my code-example:

        let is_regularfile name =
              let filekind name = let tmp = Unix.stat name in tmp.Unix.st_kind
              let fk = try (filekind name ) with
                          | Unix.Unix_error (Unix.ENOENT,_,_) -> Unix.S_DIR
                          | _ -> Unix.S_DIR

               match fk with
               | Unix.S_REG -> true
               | _          -> false;;

    ob> This example works: It tells me, if a file is of kind/type
    ob> regular file with true. If it is not a regular file or an
    ob> error occured, it tells me this by passing me the result
    ob> false.

How about:

let is_regularfile name =
  try (match Unix.stat name with
         | {Unix.st_kind = Unix.S_REG} -> true
         | _ -> false)
    _ -> false

I'm not sure why you're using so many intermediate values here--it
just seems to make the code harder to figure out.  Better to just wrap
a try around the whole check, and use a small simple match on the stat

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: