Skip to content

Instantly share code, notes, and snippets.

@yen3
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yen3/a60a975ec85f3e778173 to your computer and use it in GitHub Desktop.
Save yen3/a60a975ec85f3e778173 to your computer and use it in GitHub Desktop.
List files in a directory recursively
module ListFiles where
import System.Directory
import System.FilePath.Posix
split:: [a] -> [Bool] -> ([a], [a])
split xs bs = foldr s ([], []) (zip xs bs)
where s (d, i) (y, z) = if i then (d:y, z) else (y, d:z)
isSpecialFile :: FilePath -> Bool
isSpecialFile f = f `elem` [".", ".."]
filesComletePath :: FilePath -> [FilePath] -> [FilePath]
filesComletePath base files = map (base </>) . filter (not . isSpecialFile) $ files
getDirs :: FilePath -> IO [FilePath]
getDirs base = do c <- filesComletePath base <$> getDirectoryContents base
(dirs, files) <- split c <$> mapM doesDirectoryExist c
sub_files <- concat <$> mapM getDirs dirs
return $ files ++ sub_files
main :: IO()
main = do filepath <- getCurrentDirectory
file_list <- getDirs filepath
mapM_ putStrLn file_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment