hashrename/Main.hs

46 lines
1.2 KiB
Haskell
Raw Permalink Normal View History

2015-08-28 00:19:58 +02:00
import Control.Monad
import Data.Char (toLower)
import Text.Printf (printf)
import Crypto.Hash.SHA1 (hash)
import System.Environment (getArgs)
import System.Directory
import System.FilePath.Posix
import qualified Data.ByteString as B
getDirectoryFiles :: FilePath -> IO [FilePath]
2015-11-22 03:27:20 +01:00
getDirectoryFiles path = map (path </>) <$>
getDirectoryContents path >>= filterM doesFileExist
2015-08-28 00:19:58 +02:00
fileHash :: FilePath -> IO String
fileHash = fmap (hex . hash) . B.readFile
hex :: B.ByteString -> String
hex bytes = B.unpack bytes >>= printf "%02X"
newName :: FilePath -> IO FilePath
newName path = do
hash <- take 10 <$> fileHash path
let (file, ext) = splitExtension path
(dir, base) = splitFileName file
new = dir </> hash <.> map toLower ext
if null base
then return path
else return new
2015-11-22 03:27:20 +01:00
main :: IO ()
2015-08-28 00:19:58 +02:00
main = do
args <- getArgs
path <- case args of
[] -> getCurrentDirectory
x:_ -> return x
putStrLn ("Renaming files in " ++ path)
files <- getDirectoryFiles path
names <- mapM newName files
forM (zip files names) $ \(x, y) -> do
let x' = takeFileName x
y' = takeFileName y
putStrLn (printf "%s -> %s" x' y')
renameFile x y
2015-11-22 03:27:20 +01:00
putStrLn "Files renamed successfully"