Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Last active December 22, 2015 20:29
Show Gist options
  • Save MgaMPKAy/6526584 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/6526584 to your computer and use it in GitHub Desktop.
List recursively all files under the giving path.
import System.Directory
-- For Hugs
(</>) :: FilePath -> FilePath -> FilePath
parent </> child = parent ++ "/" ++ child
fix f = let x = f x in x
when b act = if b then act else return ()
-- Print file and directory recursively
main = fix (\f dir -> getDirectoryContents dir >>= (mapM_ (\d -> putStrLn d >> doesDirectoryExist (dir </> d) >>= flip when (f $ dir </> d))) . filter (`notElem` [".", ".."])) "/tmp"
import Control.Applicative ((<$>))
import System.Directory
-- Because Hugs doesn't have System.FilePath.(</>)
(</>) :: FilePath -> FilePath -> FilePath
parent </> child = parent ++ "/" ++ child
getDirectoryContents' :: FilePath -> IO [FilePath]
getDirectoryContents' dir =
filter (`notElem` [".", ".."]) <$> getDirectoryContents dir
recursiveFileList :: FilePath -> IO [FilePath]
recursiveFileList path = do
isDir <- doesDirectoryExist path
if not isDir
then do
isFile <- doesFileExist path
if isFile
then return [path]
else return []
else do
subPaths <- map (path </>) <$> getDirectoryContents' path
subFiles <- mapM recursiveFileList subPaths
return $ concat subFiles
-- List files and directories:
-- return $ path : concat subFiles
main = recursiveFileList "/tmp/oldu/packages" >>= mapM_ putStrLn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment