[Haskell-cafe] [Beginner's Question] How to read filenames from
a DirStream
David Brown
haskell2 at davidb.org
Mon Apr 9 14:45:21 EDT 2007
Albert Lee wrote:
> and I write that in haskell:
>
> -----
> import System.Posix
> import System.IO
>
> main = do
> dp <- openDirStream "/"
> df <- readDirStream dp
> putStrLn df
> closeDirStream dp
Some code snippets out of harchive, which uses DirStream:
bracket (openDirStream path) closeDirStream (getNames [])
-- Extract the names in the given DirStream, eliminating "." and "..",
-- and sorting the result.
getNames :: [String] -> DirStream -> IO [String]
getNames names str = do
name <- readDirStream str
case name of
"" -> return (sort names)
"." -> getNames names str
".." -> getNames names str
_ -> getNames (name:names) str
You may not need the names sorted. This also may hold onto some
memory on large directories, I haven't looked if something might need
to be made more strict.
Dave
More information about the Haskell-Cafe
mailing list