Alex Rivera | Logout

how to list all files in current directory

Asked 2011-07-11T11:22:35.660
14

What I want is to write a Haskell function to return the files of current directory e.g

Change the current directory to

 :cd c:/code/haskell

Then write a function which returns the files in a set e.g

 [x | x <-getDirectoryContents ]

Edited:

I have wrote a function sth like this which lists files (ref: http://zvon.org/other/haskell/Outputdirectory/index.html)

import Directory 

main = _dir "/tmp/FOO"

_dir _path =do
    setCurrentDirectory _path
    _cd <- getCurrentDirectory
    print _cd
    _file <- getDirectoryContents _cd
    print _file

so calling _dir "c:/code/haskell" will list all files + directory names (non-recursive) . What I want now is to call this in a predicate function, for example:

[ x| x <- _dir  "c:/code/haskell" | x start with 'haskell_' ]  

so I can apply a filter on file name

Edit
Report

1 Answer

24

It seems you are looking for:

getDirectoryContents :: FilePath -> IO [FilePath]

Refer : http://www.haskell.org/ghc/docs/6.12.2/html/libraries/directory-1.0.1.1/System-Directory.html#1

answered 2011-07-11T11:26:37.000

Your Answer