Created
November 16, 2012 06:50
-
-
Save xdcrafts/4084892 to your computer and use it in GitHub Desktop.
Utility that copies all files from source directory that absent in target directory.
This file contains 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 Main (main) where | |
import System.Environment (getArgs) | |
import System.Directory (getDirectoryContents, doesFileExist, copyFile) | |
import Control.Monad (filterM, mapM) | |
import Data.List | |
-- Line separator | |
lineSeparator = "\n" | |
-- Not proper names | |
bannedNames = ["..", "."] | |
-- Info string | |
usageString = "Usage: [source directory] [target directory]" | |
-- Function that appends directory name to file's name | |
appendDirectoryToFileName :: String -> String -> String | |
appendDirectoryToFileName directory fileName = directory ++ "/" ++ fileName | |
-- Function that gets only file contents from directory. | |
getProperFileNames :: String -> IO [FilePath] | |
getProperFileNames directoryName = do | |
directoryContents <- getDirectoryContents directoryName | |
let properNames = | |
(filter | |
(\name -> name `notElem` bannedNames) | |
directoryContents) | |
filterM (\name -> doesFileExist (appendDirectoryToFileName directoryName name)) properNames | |
-- Main function | |
main :: IO () | |
main = do | |
arguments <- getArgs | |
let argsLength = length arguments | |
if argsLength == 2 then do | |
let sourceDirectoryName = arguments !! 0 | |
let targetDirectoryName = arguments !! 1 | |
putStrLn $ "Copying files from: \"" ++ sourceDirectoryName ++ "\" to \"" ++ targetDirectoryName ++ "\"" | |
properSourceFileNames <- getProperFileNames sourceDirectoryName | |
properTargetFileNames <- getProperFileNames targetDirectoryName | |
let filesDifference = properSourceFileNames \\ properTargetFileNames | |
putStrLn $ foldl (\first second -> first ++ lineSeparator ++ second) "" filesDifference | |
copyResults <- mapM | |
(\name -> do | |
copyFile | |
(appendDirectoryToFileName sourceDirectoryName name) | |
(appendDirectoryToFileName targetDirectoryName name)) | |
filesDifference | |
putStrLn $ (show $ length copyResults) ++ " files successfuly copied." | |
else | |
error usageString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment