Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created August 28, 2015 00:33
Show Gist options
  • Save afonsomatos/35920703efb662c8078a to your computer and use it in GitHub Desktop.
Save afonsomatos/35920703efb662c8078a to your computer and use it in GitHub Desktop.
Get files recursively inside directories
module System.Directory.Tree where
import System.Directory ( doesFileExist, getDirectoryContents )
data DirTree = Directory String [DirTree]
| File String
deriving (Show, Eq)
getDirTree :: FilePath -> IO [DirTree]
getDirTree path = getDirectoryContents path
>>= mapM dirOrFile
. filter (`notElem` [".", ".."])
where dirOrFile n = let loc = path ++ "/" ++ n
tree True = fmap (Directory n) $ getDirTree loc
tree False = return $ File n
in doesFileExist loc
>>= tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment