Tree package:directory-tree

Provides a simple data structure mirroring a directory tree on the filesystem, as well as useful functions for reading and writing file and directory structures in the IO monad. Errors are caught in a special constructor in the DirTree type. Defined instances of Functor, Traversable and Foldable allow for easily operating on a directory of files. For example, you could use Foldable.foldr to create a hash of the entire contents of a directory. The functions readDirectoryWithL and buildL allow for doing directory-traversing IO lazily as required by the execution of pure code. This allows you to treat large directories the same way as you would a lazy infinite list. The AnchoredDirTree type is a simple wrapper for DirTree to keep track of a base directory context for the DirTree. Please send me any requests, bugs, or other feedback on this module!
A simple directory-like tree datatype, with useful IO functions A simple directory-like tree datatype, with useful IO functions and Foldable and Traversable instance Provides a simple data structure mirroring a directory tree on the filesystem, as well as useful functions for reading and writing file and directory structures in the IO monad. Importing the library and optional (useful) Foldable and Traverable libraries:
import System.Directory.Tree
import qualified Data.Foldable as F
import qualified Data.Traversable as T
Write a hand-made directory tree of textfiles (strings) to the disk. Simulates creating a new user Tux's home directory on a unix machine:
writeDirectory$ "/home" :/ Dir "Tux" [File "README" "Welcome!"]
"read" a directory by opening all the files at a filepath with readFile, returning an 'AnchoredDirTree String' (d2). Then check for any IO failures:
do (base :/ d2) <- readDirectory "../parent_dir/dir2/"
let failed = anyFailed d2
if failed then ...
Use Foldable instance function to concat a directory dir of text files into a single file under the same directory:
do (b :/ dt) <- readDirectory dir
let f = F.concat dt
return$ b :/ File "ALL_TEXT" f
Open all the files in the current directory as lazy bytestrings, ignoring the base path in Anchored wrapper:
import qualified Data.ByteString.Lazy as B
do (_ :/ dTree) <- readDirectoryWith B.readFile "./"
This version also offers an experimental function readDirectoryWithL that does lazy directory IO, allowing you to treat the returned DirTree as if it were a normal lazily-generated data structure. For example, the following does only the amount of IO necessary to list the file names of the children of the root directory, similar to "ls /":
do d <- readDirectoryWithL readFile "/"
mapM_ (putStrLn . name) $ contents $ free d
Any ideas or suggestions for improvements are most welcome :-) CHANGES: from 0.11
a simple wrapper to hold a base directory name, which can be either an absolute or relative path. This lets us give the DirTree a context, while still letting us store only directory and file names (not full paths) in the DirTree. (uses an infix constructor; don't be scared)
the String in the name field is always a file name, never a full path. The free type variable is used in the File constructor and can hold Handles, Strings representing a file's contents or anything else you can think of. We catch any IO errors in the Failed constructor. an Exception can be converted to a String with show.