Last active
August 17, 2018 21:06
-
-
Save alogic0/19b208c582c81a2908bb3c1f1688e48f to your computer and use it in GitHub Desktop.
Using regex-applicative for renaming files
This file contains hidden or 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 where | |
import Text.Regex.Applicative | |
import Text.Regex.Applicative.Common | |
import System.Environment | |
import Control.Monad.State | |
-- transformation example: "4_CAM 6_main_20180801101345_20180801101432.avi" | |
-- --> "20180801101345_20180801101432_4_CAM_6.avi" | |
-- or: "5_IPC_main_20160807164154_20160807164247.avi" | |
-- --> "20160807164154_20160807164247_5_IPC.avi" | |
infixr 5 +++ | |
(+++) :: RE Char String -> RE Char String -> RE Char String | |
(+++) = liftA2 (++) | |
dt = concatMap show <$> many digit | |
pt1 = foldr1 (+++) [dt | |
, string "_" | |
, string "C" <|> string "K" | |
, string "AM" | |
, const "_" <$> many (sym ' ') | |
, dt <* string "_main_"] | |
<|> (dt +++ string "_IPC" <* string "_main_") | |
pt2 = dt +++ string "_" +++ dt | |
{- | |
rn s = do | |
(p1, rs) <- findFirstPrefix pt1 s | |
(p2, rs2) <- findFirstPrefix pt2 rs | |
return $ p2 ++ "_" ++ p1 ++ rs2 | |
-} | |
rn = do | |
p1 <- newSt pt1 | |
p2 <- newSt pt2 | |
rs <- get | |
return $ p2 ++ "_" ++ p1 ++ rs | |
where newSt re = StateT $ findFirstPrefix re | |
main :: IO () | |
main = do | |
args <- getArgs | |
guard $ length args == 1 | |
let inputStr = args !! 0 | |
let s1 = maybe inputStr id (evalStateT rn inputStr) | |
putStrLn s1 |
This file contains hidden or 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
#!/bin/bash | |
find . -name "*.avi" | while read i; do mv -v "$i" $(rn-vid "$(basename "$i")"); done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment