3

My family has recordings of the children's performances in school and extracurricular activities saved over the years as primarily .avi and .mp4 files. I have been asked to take all these files (there are well over a hundred of them) and relabel any video that starts with the word The or A and move it to the end.

For example, a video titled The Kids Performances (2020).mp4 would be changed to Kids Performances, The (2020).mp4. I have no idea why they want to keep the words The and 'A' in the titles at all, but they do.

I’m using Windows and found a relatively simple method on a board on this site that I can use to get rid of the The at the beginning of the titles, but I cannot figure out a way to move it to the back of the title before the year recorded which is always listed as (year).

Here’s what I am talking about:

for %%i in ("*.mp4") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off 1st four chars, then appends prefix
ren "%fname%" "%fname:~4%"
goto :eof

This takes care of removing the first 4 characters from the titles in a batch format, but I am having trouble moving those first 4 characters to the end of the title before the year. I have pleaded with my family members to just leave these words out of the title all together, but I cannot convince them.

I have Python installed on the PC as well if that would provide an easier method to achieve this goal. Thank you in advance to anyone able to assist!

7
  • Do all the files have (YYYY) at the end of the name?
    – DavidPostill
    Apr 20, 2021 at 20:48
  • Would this be as expected? s3.gifyu.com/images/The_And_A.png Apr 20, 2021 at 23:39
  • You sir get a +1 from me just for using call and goto :EOF (preventing the need for delayed expansion regardless of what you do in the labeled block). Apr 21, 2021 at 20:07
  • Yes, all the files have (YYYY) at the end of the name.
    – olpdog
    Apr 22, 2021 at 2:10
  • s3.gifyu.com/images/The_And_A.png – Ricardo Bohner YES that is exactly what I am looking for!!!
    – olpdog
    Apr 22, 2021 at 3:55

2 Answers 2

9

PowerShell with easily expandable Article list & extension list (We can use -whatIf for testing and then later delete it to execute our changes.)

$ArticleList = @( 'A' , 'The' )
$ExtList     = @( '*.avi' , '*.mp4' )
# From folder with files
# Remove "-WhatIf" to execute after testing
gci $ExtList | 
   ? { $_.BaseName -match ( '^({0})\s(\.+)\s(\(\d+\))' -f ( $ArticleList -join '|' )) } | 
       ReName-Item -NewName { '{0}, {1} {3}{4}' -f $matches[2] , $matches[1] , $matches[3] , $_.Extension -WhatIf }

'^({0})\s(\.+)\s(\(\d+\))' -f ( $ArticleList -join '|' )

creates a regex

'^(Article[0]|Article[1]{|...})\s(\.+)\s(\(\d+\))'

with three matching groups:

  • Article : ({0})
  • TitleTExt: \s(\.+)
  • (Year) : (\(\d+\))

used to construct the newname.

0
0

Not an automated solution as the one posted by Keith Miller, but as someone who wrote enough cmd scripts to know I don’t want to write another one, I would use Excel for this:

  1. Open the cmd prompt and navigate to the folder with the files to rename.
  2. Copy the list of filename to the clipboard using dir /b | clip (or dir /b /s | clip if the files are saved in subdirectories) and paste this into Excel.
  3. Save the new filenames into another column using whatever formulas you need. If you cannot do it fully automatically you can always tweak the results manually where necessary.
  4. Once you are happy with the new filenames make an Excel formula for renaming the files one by one. At this stage the sheet could look similar to:
A B C D E F G
1 Formulas:
2 Original filename Prefix Year Extension Rest of filename New filename Command
3 A Kids Performance (2018).avi =IF(OR(UPPER(LEFT(A3;2))="A ";UPPER(LEFT(A3;4))="THE ");LEFT(A3;FIND(" ";A3)-1);"") =MID(A3;FIND("(";A3)+1;4) =RIGHT(A3;3) =MID(A3;IF(B3="";1;LEN(B3)+2);LEN(A3)-LEN(B3)-LEN(C3)-LEN(D3)-IF(B3="";4;5)) =E3&IF(B3="";"";", "&B3)&" ("&C3&")."&D3 ="ren """&A3&""" """&F3&""""
4 Some Kids Performance (2019).mkv =IF(OR(UPPER(LEFT(A4;2))="A ";UPPER(LEFT(A4;4))="THE ");LEFT(A4;FIND(" ";A4)-1);"") =MID(A4;FIND("(";A4)+1;4) =RIGHT(A4;3) =MID(A4;IF(B4="";1;LEN(B4)+2);LEN(A4)-LEN(B4)-LEN(C4)-LEN(D4)-IF(B4="";4;5)) =E4&IF(B4="";"";", "&B4)&" ("&C4&")."&D4 ="ren """&A4&""" """&F4&""""
5 The Kids Performances (2020).mp4 =IF(OR(UPPER(LEFT(A5;2))="A ";UPPER(LEFT(A5;4))="THE ");LEFT(A5;FIND(" ";A5)-1);"") =MID(A5;FIND("(";A5)+1;4) =RIGHT(A5;3) =MID(A5;IF(B5="";1;LEN(B5)+2);LEN(A5)-LEN(B5)-LEN(C5)-LEN(D5)-IF(B5="";4;5)) =E5&IF(B5="";"";", "&B5)&" ("&C5&")."&D5 ="ren """&A5&""" """&F5&""""
1 Values:
2 Original filename Prefix Year Extension Rest of filename New filename Command
3 A Kids Performance (2018).avi A 2018 avi Kids Performance Kids Performance, A (2018).avi ren "A Kids Performance (2018).avi" "Kids Performance, A (2018).avi"
4 Some Kids Performance (2019).mkv 2019 mkv Some Kids Performance Some Kids Performance (2019).mkv ren "Some Kids Performance (2019).mkv" "Some Kids Performance (2019).mkv"
5 The Kids Performances (2020).mp4 The 2020 mp4 Kids Performances Kids Performances, The (2020).mp4 ren "The Kids Performances (2020).mp4" "Kids Performances, The (2020).mp4"
  1. Select the cells with the calculated commands (in the above example G3:G5) and copy them to put them into the clipboard and either right click in the command prompt window (you might need to press enter on the last line) or paste the clipboard contents in an empty *.cmd or *.bat file, save it the folder with the files to be renamed and run it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .