-import System.Directory (getDirectoryContents)
-import Data.List (sort, isPrefixOf, isSuffixOf)
+import System.Directory
+import Control.Exception
+import System.FilePath
+import Data.List
+
+dir = "getDirContents001.dir"
main = do
- names <- getDirectoryContents "."
- putStrLn (unlines (sort (filter ok names)))
+ try cleanup :: IO (Either IOException ())
+ bracket (createDirectory dir) (const cleanup) $ \_ -> do
+ getDirectoryContents dir >>= print . sort
+ mapM_ (\s -> writeFile (dir </> ('f':show s)) (show s)) [1..100]
+ getDirectoryContents dir >>= print . sort
-ok name = "getDirContents" `isPrefixOf` name
- && not ("bak" `isSuffixOf` name)
- && not ("prof" `isSuffixOf` name)
- && not ("hp" `isSuffixOf` name)
- && not ("ps" `isSuffixOf` name)
- && not ("aux" `isSuffixOf` name)
+cleanup = do
+ files <- getDirectoryContents dir
+ mapM_ (removeFile . (dir </>)) (filter (not . ("." `isPrefixOf`)) files)
+ removeDirectory dir
-getDirContents001
-getDirContents001.comp.stderr
-getDirContents001.hi
-getDirContents001.hs
-getDirContents001.o
-getDirContents001.run.stderr
-getDirContents001.run.stdout
-getDirContents001.stdout
-getDirContents001.stdout-i386-unknown-mingw32
-
+[".",".."]
+[".","..","f1","f10","f100","f11","f12","f13","f14","f15","f16","f17","f18","f19","f2","f20","f21","f22","f23","f24","f25","f26","f27","f28","f29","f3","f30","f31","f32","f33","f34","f35","f36","f37","f38","f39","f4","f40","f41","f42","f43","f44","f45","f46","f47","f48","f49","f5","f50","f51","f52","f53","f54","f55","f56","f57","f58","f59","f6","f60","f61","f62","f63","f64","f65","f66","f67","f68","f69","f7","f70","f71","f72","f73","f74","f75","f76","f77","f78","f79","f8","f80","f81","f82","f83","f84","f85","f86","f87","f88","f89","f9","f90","f91","f92","f93","f94","f95","f96","f97","f98","f99"]