Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.IO (openTempFile, hClose)
- import System.Directory (getCurrentDirectory, getDirectoryContents, removeFile,
- renameFile)
- import System.Posix.Files (FileStatus, isDirectory, getFileStatus)
- import System.FilePath.Posix (replaceBaseName)
- main :: IO ()
- main = do
- cwd <- getCurrentDirectory
- getFiles cwd
- files <- getFiles cwd
- randommv files
- newfiles <- getFiles cwd
- mv newfiles 1
- getFiles :: String -> IO [String]
- getFiles cwd = do
- files <- getDirectoryContents cwd
- filesanddata <- mapM getMetaData files
- return (myfilter isDirectory filesanddata)
- getMetaData :: String -> IO (String, FileStatus)
- getMetaData file = do
- metadata <- getFileStatus file
- return (file, metadata)
- {-
- This function takes a function with takes the
- second item of the tuple and returns a bool, if
- the return of the function is true, the item is
- dropped and if it is false, the first item of the
- tuple is added to the list returned by the function
- -}
- myfilter :: (b -> Bool) -> [(a,b)] -> [a]
- myfilter _ [] = []
- myfilter f ((x,y):xs)
- | not (f y) = x : myfilter f xs
- | otherwise = myfilter f xs
- --Renames file to random name to avoid overwrites
- randommv :: [String] -> IO ()
- randommv [] = return ()
- randommv (x:xs) = do
- cwd <- getCurrentDirectory
- (tempName, tempHandle) <- openTempFile cwd "foo"
- hClose tempHandle
- removeFile tempName
- let newname = replaceBaseName x tempName
- renameFile x newname
- randommv xs
- mv :: [String] -> Int -> IO ()
- mv [] _ = return ()
- mv (x:xs) n = do
- let newname = replaceBaseName x (show n)
- renameFile x newname
- mv xs (succ n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement