-2

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

(Name changed)

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.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36

1 Answers1

3

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'

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.


### Update two years later -

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
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Was focusing on the hard part. Cameron, do you need me to remove the resolution and upcase Part? – Paul Hodges Nov 06 '18 at 14:49
  • Hard part is OK – gboffi Nov 06 '18 at 14:54
  • `Emmerdale 1st Oct 2018 1080p (Deep61) [WWRG].mp4` . Would be perfect if (Deep61) [WWRG] was removed. Great extra would be to move it into a processed folder. – Cameron Austin Nov 07 '18 at 12:22
  • By the way it worked perfectly. The odd file had a whitespace between [WWRG] and .mp4 before running the script and that stayed but that isnt a problem. – Cameron Austin Nov 07 '18 at 12:26
  • Doesn't work with `Coronation Street 2nd Nov 2018 part 2 1080p (Deep61) [WWRG].mp4` – Cameron Austin Nov 07 '18 at 13:50
  • Ah - extra fields. If you have possible embedded spaces in the "1st field" ahead of the date we have to change the logic. Let me look at it a little. A simple read will have a hard time with that, but I can likely use `sed`. – Paul Hodges Nov 07 '18 at 14:34
  • (This is why it's generally a bad idea to embed spaces in a filename...) – Paul Hodges Nov 07 '18 at 14:35
  • Try it now. This is more complicated, but ought to get you where you need to go. I used `sed`, and may have overly elaborated the patterns; just simplify them if (for example) one has the month in all caps. – Paul Hodges Nov 07 '18 at 16:07
  • Yeah. :( Hadn't occurred to be there might be multiple leading fields. Real world data trumps clever code almost every time. As Burns said To A Mouse, “The best laid schemes o' mice an' men / Gang aft a-gley.” – Paul Hodges Nov 08 '18 at 14:25
  • The new scripts didnt work. It also made bash run my CPU at 100% `Coronation Street 10th Oct 2018 part 2 1080p (Deep61) [WWRG].mp4 >> Coronation Street 10th Oct 2018 part 2 1080p (Deep61) [WWRG].mp4`2018 11 08 – Cameron Austin Nov 08 '18 at 16:49
  • Missed the h as the 2nd character of ordinals. Had it before, but must have fumble-fingered it away at some point. – Paul Hodges Nov 08 '18 at 17:07
  • That has fixed the naming perfectly, but the script now does one rename and then spikes to CPU up too 100% again. – Cameron Austin Nov 08 '18 at 18:04
  • I was using the old filename as the contents of the new filename, and that echo was reading the file. Could that be the problem? – Paul Hodges Nov 08 '18 at 20:54
  • I will be renaming <=20 and then 3-4 a day – Cameron Austin Nov 09 '18 at 01:00
  • PERFECT. You are fantastic! One last question, I have this saved in folder A. How can I run this as a cron job checking file in folder B? – Cameron Austin Nov 09 '18 at 20:39
  • Sorted. Thank you so much. You have been very patient and helpful. – Cameron Austin Nov 09 '18 at 20:43
  • if you saved it as a file, you ought to be able to `cd $somePath && /path/to/script` from anywhere, including a crontab. Try a few tests and see. If it's working for you, please accept the solution. ;) – Paul Hodges Nov 09 '18 at 21:14