[Beginner's Question] How to read filenames from a DirStream

Newbie's question: I want to ls the filenames of a directory. The program in
C is something like:
#include

Albert Lee on 2007-04-09 10:46:14 +0800:
I want to ls the filenames of a directory.
[...]
and I write that in haskell:
----- import System.Posix import System.IO
main = do dp <- openDirStream "/" df <- readDirStream dp putStrLn df closeDirStream dp
------ It can print the first entry of the dir, but how can we list it all like the C prog? map ? list comperhension?
System.Posix is one way to do it, but you might find the functions in System.Directory easier to use - there's a function to get the directory contents as a list of strings. To print them out, you may want to look at one of the mapM functions.

Thanks for your reply. and I have done it by:
import System.IO
import System.Posix
import System.Directory
show_current_dir :: IO ()
show_current_dir = do
curr_dir <- getWorkingDirectory
putStrLn ("Current Working Directory:" ++ curr_dir)
ls_dir1 :: String -> IO [()]
ls_dir1 fp = do
files <- getDirectoryContents fp
putStrLn ("ls_dir1: Files in " ++ fp ++ ":")
mapM putStrLn files
read_fp_from_DirStream :: DirStream -> IO()
read_fp_from_DirStream dp = do
f <- readDirStream dp
if f == "" then return () else do putStrLn f
read_fp_from_DirStream dp
ls_dir2 :: String -> IO ()
ls_dir2 fp = do
dp <- openDirStream fp
putStrLn ("ls_dir2: files in " ++ fp ++ ":")
read_fp_from_DirStream dp
closeDirStream dp
main :: IO ()
main = do
putStrLn "Unix programming use Haskell"
show_current_dir
ls_dir1 "/"
ls_dir2 "/home"
-- is that right?
2007/4/9, Alec Berryman
Albert Lee on 2007-04-09 10:46:14 +0800:
I want to ls the filenames of a directory.
[...]
and I write that in haskell:
----- import System.Posix import System.IO
main = do dp <- openDirStream "/" df <- readDirStream dp putStrLn df closeDirStream dp
------ It can print the first entry of the dir, but how can we list it all like the C prog? map ? list comperhension?
System.Posix is one way to do it, but you might find the functions in System.Directory easier to use - there's a function to get the directory contents as a list of strings. To print them out, you may want to look at one of the mapM functions. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Albert, Monday, April 9, 2007, 6:46:14 AM, you wrote:
It can print the first entry of the dir, but how can we list it all like the C prog? map ? list comperhension?
http://haskell.org/haskellwiki/IO_inside forever :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

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

It looks like all you can do with DirStream is get the filename, not look at any other fields of the dirent - actually, it seems name is the only standard field. You might as well use getDirectoryContents, unless you have a directory so huge that a list of all the filenames takes too much memory! Brandon Moore

Hello, This multipart tutorial seems similar to what you are describing: http://blog.moertel.com/articles/2007/03/28/directory-tree-printing-in-haske... The tutorial is actually a bit more complicated, it is traversing a whole directory tree and printing a nice graph. HTH, j. At Mon, 9 Apr 2007 10:46:14 +0800, Albert Lee wrote:
Newbie's question: I want to ls the filenames of a directory. The program in C is something like:
#include
int main(){ DIR *dp; struct dirent *dirp;
dp = opendir("/");
while((dirp = readdir(dp)) != NULL) printf("%s\n", dirp->d_name);
closedir(dp); }
and I write that in haskell:
----- import System.Posix import System.IO
main = do dp <- openDirStream "/" df <- readDirStream dp putStrLn df closeDirStream dp
------ It can print the first entry of the dir, but how can we list it all like the C prog? map ? list comperhension?
participants (7)
-
Albert Lee
-
Alec Berryman
-
Brandon Michael Moore
-
Bulat Ziganshin
-
David Brown
-
David House
-
Jeremy Shaw