Last active
August 29, 2015 14:22
-
-
Save yen3/a60a975ec85f3e778173 to your computer and use it in GitHub Desktop.
List files in a directory recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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