batch-rename 0.1.0.9 → 0.1.1.0
raw patch · 2 files changed
+16/−19 lines, 2 files
Files
- batch-rename.cabal +1/−1
- batchRename.hs +15/−18
batch-rename.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.0.9+version: 0.1.1.0 -- A short (one-line) description of the package. synopsis: Make Linux or MacOS do things like "rename *.mp3 *.mp4"
batchRename.hs view
@@ -1,19 +1,16 @@ import Data.List+import Data.Maybe import System.Directory import System.Environment import System.FilePath.Glob-import System.FilePath.Posix+import System.FilePath usage :: IO () usage = do progName <- getProgName putStrLn ("Usage:\n\t" ++ progName ++ " \"srcPattern\" \"dstPattern\"") -hasWildcard :: [Char] -> Bool-hasWildcard name = 1 == (length $ filter (\x -> x == '*') name)--bruteIndex e l = case (elemIndex e l) of- Just x -> x- Nothing -> 0+hasWildcard :: String -> Bool+hasWildcard name = 1 == length (filter (== '*') name) trimHeadTail s pre pos = let nohead = drop (length pre) s in@@ -22,20 +19,20 @@ partRename preL posL preR posR name = let fn = takeFileName name in- renameFile fn (preR ++ (trimHeadTail fn preL posL) ++ posR)+ renameFile fn (preR ++ + trimHeadTail fn preL posL +++ posR) -batchRename :: [Char] -> [Char] -> IO ()+fetchPrePos s = let idx = fromMaybe 0 (elemIndex '*' s) in+ (take idx s, drop (idx + 1) s)++batchRename :: String -> String -> IO () batchRename left right = if hasWildcard left && hasWildcard right then- let wildcardLeftIndex = bruteIndex '*' left- oldPreLeft = take wildcardLeftIndex left - oldPosLeft = drop (wildcardLeftIndex + 1) left- wildcardRightIndex = bruteIndex '*' right- oldPreRight = take wildcardRightIndex right- oldPosRight = drop (wildcardRightIndex + 1) right in+ let (oldPreLeft, oldPosLeft) = fetchPrePos left+ (oldPreRight, oldPosRight) = fetchPrePos right in do files <- glob left- sequence_ (map (partRename oldPreLeft oldPosLeft- oldPreRight oldPosRight)- files)+ mapM_ (partRename oldPreLeft oldPosLeft oldPreRight oldPosRight)+ files else putStrLn "Oops, use simple rename please." main :: IO ()