更改文件名日期格式 [英] Changing filename date format

查看:125
本文介绍了更改文件名日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文件无法被 filebot 识别,因为它们具有以下命名方案.

I have some files that are not recognised by filebot because they have the following naming scheme.

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

(改名)

然而,Filebot 确实识别出这种命名方案并需要

Filebot does however recognize this naming scheme and needs

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.

推荐答案

使用 read from a here-string 将文件名解析为字段,然后使用 date 重新格式化.

Use read from a here-string to parse the filename into fields, then use date to reformat.

更新以处理日期之前元素之间的空格.

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'

请注意,我在 echo 中使用了单引号,并且 ls 将它们放在其输出的周围,但它们不是文件名的一部分.

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.

我从不喜欢在循环的每次迭代中生成那个 read/sed 对.如果你有几十万个文件,那会变得非常慢.这是替代方案的内核.

I never liked spawning that read/sed pair on every iteration of the loop. If you had a a 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

这篇关于更改文件名日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆