Loading ...
Sorry, an error occurred while loading the content.

list all files in a directory

Expand Messages
  • Jerry He
    Hi, Does anyone know how to list all files in a directory in Ocaml? My goal is to be able to open each file in a directory and process one by one without
    Message 1 of 4 , Jul 28, 2005
    • 0 Attachment
      Hi,
      Does anyone know how to list all files in a
      directory in Ocaml? My goal is to be able to open each
      file in a directory and process one by one without
      knowing the names of those files.

      thanks in advance,

      Jerry

      __________________________________________________
      Do You Yahoo!?
      Tired of spam? Yahoo! Mail has the best spam protection around
      http://mail.yahoo.com
    • Alan Post
      ... http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html
      Message 2 of 4 , Jul 28, 2005
      • 0 Attachment
        In article <20050729021206.47259.qmail@...>, Jerry He wrote:
        >
        > Does anyone know how to list all files in a
        > directory in Ocaml? My goal is to be able to open each
        > file in a directory and process one by one without
        > knowing the names of those files.

        http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html
      • Richard Jones
        ... The easiest way is to use Sys.readdir, which takes the name of the directory and returns an array containing the names of the files in the directory
        Message 3 of 4 , Jul 29, 2005
        • 0 Attachment
          On Thu, Jul 28, 2005 at 07:12:06PM -0700, Jerry He wrote:
          > Does anyone know how to list all files in a
          > directory in Ocaml? My goal is to be able to open each
          > file in a directory and process one by one without
          > knowing the names of those files.

          The easiest way is to use Sys.readdir, which takes the name of the
          directory and returns an array containing the names of the files in
          the directory (excluding "." and ".."). eg:

          $ ocaml
          Objective Caml version 3.08.2

          # Sys.readdir ".";;
          - : string array =
          [|".bashrc"; ".bash_profile"; ".Xauthority"; (* etc *) |]

          If you want a list, use Array.to_list (Sys.readdir ".").

          A more flexible, but more convoluted approach is to use the Unix-
          module functions Unix.opendir, Unix.readdir and Unix.closedir.
          (Despite the name, they should work on Windows too).

          Note that neither of these functions will necessarily return the names
          in alphabetical order. You may want to sort the filenames if you're
          going to present results back to the user.

          Rich.

          --
          Richard Jones, CTO Merjis Ltd.
          Merjis - web marketing and technology - http://merjis.com
          Team Notepad - intranets and extranets for business - http://team-notepad.com
        • Martin Jambon
          Here is some code which does this (taken from the micmatch library) and provides some options. See http://martin.jambon.free.fr/mmodoc/Micmatch.Directory.html
          Message 4 of 4 , Aug 1, 2005
          • 0 Attachment
            Here is some code which does this (taken from the micmatch library) and
            provides some options.
            See http://martin.jambon.free.fr/mmodoc/Micmatch.Directory.html for the
            documentation.

            let list ?(absolute = false) ?path dir =
            let names = Sys.readdir dir in
            Array.sort String.compare names;

            let make_path, path_maker =
            match absolute, path with
            false, None
            | false, Some false -> false, (fun s -> s)
            | false, Some true -> true, Filename.concat dir
            | true, Some true
            | true, None ->
            let f =
            if Filename.is_relative dir then
            let cwd = Sys.getcwd () in
            Filename.concat (Filename.concat cwd dir)
            else Filename.concat dir in
            true, f
            | true, Some false -> invalid_arg "Directory.list" in

            let paths =
            if make_path then Array.map path_maker names
            else names in
            Array.to_list paths



            Martin


            On Fri, 29 Jul 2005, Richard Jones wrote:

            > On Thu, Jul 28, 2005 at 07:12:06PM -0700, Jerry He wrote:
            >> Does anyone know how to list all files in a
            >> directory in Ocaml? My goal is to be able to open each
            >> file in a directory and process one by one without
            >> knowing the names of those files.
            >
            > The easiest way is to use Sys.readdir, which takes the name of the
            > directory and returns an array containing the names of the files in
            > the directory (excluding "." and ".."). eg:
            >
            > $ ocaml
            > Objective Caml version 3.08.2
            >
            > # Sys.readdir ".";;
            > - : string array =
            > [|".bashrc"; ".bash_profile"; ".Xauthority"; (* etc *) |]
            >
            > If you want a list, use Array.to_list (Sys.readdir ".").
            >
            > A more flexible, but more convoluted approach is to use the Unix-
            > module functions Unix.opendir, Unix.readdir and Unix.closedir.
            > (Despite the name, they should work on Windows too).
            >
            > Note that neither of these functions will necessarily return the names
            > in alphabetical order. You may want to sort the filenames if you're
            > going to present results back to the user.
            >
            > Rich.
            >
            > --
            > Richard Jones, CTO Merjis Ltd.
            > Merjis - web marketing and technology - http://merjis.com
            > Team Notepad - intranets and extranets for business - http://team-notepad.com
            >
            >
            >
            > Archives up to September 30, 2004 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
            > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
            > Attachments are banned and you're asked to be polite, avoid flames etc.
            > Yahoo! Groups Links
            >
            >
            >
            >
            >
            >
            >

            --
            Martin Jambon, PhD
            http://martin.jambon.free.fr

            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.