Advertisement
Guest User

Untitled

a guest
Dec 29th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO (openTempFile, hClose)
  2. import System.Directory (getCurrentDirectory, getDirectoryContents, removeFile,
  3.                             renameFile)
  4. import System.Posix.Files (FileStatus, isDirectory, getFileStatus)
  5. import System.FilePath.Posix (replaceBaseName)
  6.  
  7. main :: IO ()
  8. main = do
  9.     cwd <- getCurrentDirectory
  10.     getFiles cwd
  11.     files <- getFiles cwd
  12.     randommv files
  13.     newfiles <- getFiles cwd
  14.     mv newfiles 1
  15.  
  16. getFiles :: String -> IO [String]
  17. getFiles cwd = do
  18.     files <- getDirectoryContents cwd
  19.     filesanddata <- mapM getMetaData files
  20.     return (myfilter isDirectory filesanddata)
  21.  
  22. getMetaData :: String -> IO (String, FileStatus)
  23. getMetaData file = do
  24.     metadata <- getFileStatus file
  25.     return (file, metadata)
  26.  
  27. {-
  28. This function takes a function with takes the
  29. second item of the tuple and returns a bool, if
  30. the return of the function is true, the item is
  31. dropped and if it is false, the first item of the
  32. tuple is added to the list returned by the function
  33. -}
  34.  
  35. myfilter :: (b -> Bool) -> [(a,b)] -> [a]
  36. myfilter _ [] = []
  37. myfilter f ((x,y):xs)
  38.     | not (f y) = x : myfilter f xs
  39.     | otherwise = myfilter f xs
  40.  
  41. --Renames file to random name to avoid overwrites
  42. randommv :: [String] -> IO ()
  43. randommv [] = return ()
  44. randommv (x:xs) = do
  45.     cwd <- getCurrentDirectory
  46.     (tempName, tempHandle) <- openTempFile cwd "foo"
  47.     hClose tempHandle
  48.     removeFile tempName
  49.     let newname = replaceBaseName x tempName
  50.     renameFile x newname
  51.     randommv xs
  52.  
  53. mv :: [String] -> Int -> IO ()
  54. mv [] _ = return ()
  55. mv (x:xs) n = do
  56.     let newname = replaceBaseName x (show n)
  57.     renameFile x newname
  58. mv xs (succ n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement