[Haskell] Reading a directory tree
glynn.clements at virgin.net
Tue Jun 22 11:56:34 EDT 2004
Tom Hofte wrote:
> I'm looking for a way to iteratively read all the files in a directory
> its subdirectories, given the filepath of the top-level dir.
> For example, I want to find a file, corresponding to a given filename,
> in a directory and its subdirectories.
> Is there a way to implement this in Haskell?
BTW, one other caveat (which applies to all of the examples so far):
doesDirectoryExist doesn't distinguish between directories and
symlinks to directories. Consequently, any directory-traversal
algorithm which uses doesDirectoryExist to identify directories will
behave incorrectly in the presence of symlinks. In the worst case, you
can get into an infinite loop.
The only alternative is to use the functions from the Posix library
(getSymbolicLinkStatus and isDirectory) instead, e.g.:
> import Posix
> doesDirectoryReallyExist :: FilePath -> IO Bool
> doesDirectoryReallyExist path = do
> stat <- getSymbolicLinkStatus
> return $ isDirectory stat
Glynn Clements <glynn.clements at virgin.net>
More information about the Haskell