简体   繁体   English

更改文件名日期格式

[英]Changing filename date format

I have some files that are not recognised by filebot because they have the following naming scheme.我有一些 filebot 无法识别的文件,因为它们具有以下命名方案。

SOAP 3rd Oct 2018 part 1 1080p (USER) [GROUP].mp4

(Name changed) (名称已更改)

Filebot does however recognize this naming scheme and needs然而,Filebot 确实认识到这种命名方案和需求

SOAP 2018 10 01 Part 1 (USER) [GROUP].mp4

I need a script that can rename these files according to that date format.我需要一个可以根据该日期格式重命名这些文件的脚本。 I have tried reading up on regular expressions but it is going straight over my head.我曾尝试阅读正则表达式,但它直接在我的脑海中浮现。 I want to understand how this can be solved.我想了解如何解决这个问题。

Use read from a here-string to parse the filename into fields, then use date to reformat.使用read from a here-string 将文件名解析为字段,然后使用date重新格式化。

Updated to handle spaces between elements before the date.更新以处理日期之前元素之间的空格。

$: for f in *mp4
   do IFS='!' read a date b <<< "$( echo "$f" | sed -E '
        s/^(.+) ([0-9]{1,2})[snrt][tdh] ([JFMASOND][aepuco][nbrylgptvc]) ([0-9]{4}) (.*)/\1!\3-\2-\4!\5/' )"
      newName="$( date -d "$date" +"$a %Y %m %d $b" )"
      echo "'$f' -> '$newName'"
      mv "$f" "$newName"
      ls -1 "$newName"
   done
'Coronation Street 2nd Nov 2018 part 2 1080p (Deep61) [WWRG].mp4' -> 'Coronation Street 2018 11 02 part 2 1080p (Deep61) [WWRG].mp4'
'Coronation Street 2018 11 02 part 2 1080p (Deep61) [WWRG].mp4'
'SOAP 3rd Oct 2018 part 1 1080p (USER) [GROUP].mp4' -> 'SOAP 2018 10 03 part 1 1080p (USER) [GROUP].mp4'
'SOAP 2018 10 03 part 1 1080p (USER) [GROUP].mp4'

Note that I use single quotes inside my echo, and ls puts them around its output, but they are not part of the name of the file.请注意,我在我的 echo 中使用了单引号,并且ls将它们放在其输出周围,但它们不是文件名称的一部分。


### Update two years later - ### 两年后更新 -

I never liked spawning that read/sed pair on every iteration of the loop.我从不喜欢在循环的每次迭代中生成read/sed对。 If you had aa couple hundred thousand files, that would get intolerably slow.如果你有几十万个文件,那会变得非常慢。 Here's the kernel of an alternative.这是替代方案的内核。

 for f in *mp4 do if [[ "$f" =~ ^(.+)\\ ([0-9]{1,2})[snrt][tdh]\\ ([JFMASOND][aepuco][nbrylgptvc])\\ ([0-9]{4})\\ (.*) ]] then echo "with Bash Match: " newName="$( date -d "${BASH_REMATCH[3]}-${BASH_REMATCH[2]}-${BASH_REMATCH[4]}" +"${BASH_REMATCH[1]} %Y %m %d ${BASH_REMATCH[5]}" )" echo "oldName:'$f' -> newName:'$newName'" else echo skipping "'$f'" fi done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM