Last active
January 14, 2018 01:23
-
-
Save cbzehner/c3a2339d52defa89608b931161169221 to your computer and use it in GitHub Desktop.
Trying to create a set of functions that will get all the files in directory and all of it's subdirectories and so on and so forth
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 Files | |
( getFilesRecursively | |
) where | |
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory) | |
import System.Exit | |
import System.FilePath (combine) | |
data PathType = Directory | File | Invalid | |
deriving (Eq, Ord, Show, Enum) | |
getFilesRecursively :: FilePath -> IO [FilePath] | |
getFilesRecursively path = do | |
pathType <- getPathType path | |
case pathType of | |
Invalid -> exitFailure -- TODO: Better error message on invalid paths | |
File -> pure [path] | |
Directory -> pure [path, path] -- TODO: Write a recursive function to get directory contents | |
getFilesFromDirectory :: FilePath -> IO [FilePath] | |
getFilesFromDirectory path = map (combine path) <$> listDirectory path | |
getFileTypeTuple :: FilePath -> IO (FilePath, PathType) | |
getFileTypeTuple path = | |
fmap (map (\p -> do | |
pathType <- getPathType | |
(p, pathType))) | |
(getFilesFromDirectory path) | |
getPathType :: FilePath -> IO PathType | |
getPathType path = do | |
directoryExists <- doesDirectoryExist path | |
fileExists <- doesFileExist path | |
if directoryExists | |
then pure Directory | |
else if fileExists | |
then pure File | |
else pure Invalid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error Message