0

I've got the following pattern :

[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv

I'd like to only keep : Title - 1234

The AAAAAA parts are always the same : 10 letters for the first set, and 4 numbers + 1 letter for the second set.

The Title - 1234 part changes constantly. The [Something that changes] part changes constantly.

Is there a way to remove what's before ] and after (, so that I only keep the Title and the Episode n° ?

I don't mind making multiple "rename" commands (I do that in batch anyway)

Any way to do that ?

Thanks !

2 Answers2

0

Use the -replace regex operator:

Get-ChildItem -Filter *.mkv |Rename-Item -NewName {"$($_.BaseName -replace '^\[\p{L}{10}\]\s*([^\(]+).*$','$1').mkv"}

The pattern used

^\[\p{L}{10}\]\s*([^\(]+).*$

... matches:

^              # Start of string
 \[            # Literal `[`
 \p{L}{10}     # 10 consecutive letters
 \]            # Literal `]`
 \s*           # Any whitespace
 ([^\(]+)      # Capture the longest possible sequence on non-`(` chars
 .*            # the rest of the string (eg. `(ABCD1) something something.mkv`)
$              # End of string

We then replace the whole thing with everything we captured in between the ] and (.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks ! One question though : is it not possible to do it via cmd with the rename function ? I'm asking that cause I plan on using it as part of a batch file (.bat) so I'd rather avoid having to launch powershell everytime (unless it's the only option) ^^" – Samoussa Binou Mymusicisinme Apr 19 '21 at 16:42
  • @SamoussaBinouMymusicisinme I'm afraid I wouldn't know how to do it in cmd/batch :) – Mathias R. Jessen Apr 19 '21 at 16:52
0

use this string substitute:

This find the first "]" and trim everything from beginning Then trim all after the first "("

@echo off

setlocal enabledelayedexpansion

for %%F in (*.mkv) do (
   set "f=%%~F"
   set "f=!f:*]=!"
   call :trim
   echo ren "%%~F" "!f!%%~xF"
)

pause
goto :eof

:trim
SET f=%f:(=&rem.%
goto :eof

output:

C:\Users\fposc\AppData\Local\Temp>ren1
ren "[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv" " Title - 1234 .mkv"
Premere un tasto per continuare . . .

If you want to remove leading and trailing spaces you can include them in the code like this:

@echo off

setlocal enabledelayedexpansion

for %%F in (*.mkv) do (
   set "f=%%~F"
   set "f=!f:*] =!"
   call :trim
   echo ren "%%~F" "!f!%%~xF"
)

pause
goto :eof

:trim
SET f=%f: (=&rem.%
goto :eof

output:

C:\Users\fposc\AppData\Local\Temp>ren1
ren "[AAAAAA] Title - 1234 (AAAAAA)[Something that changes].mkv" "Title - 1234.mkv"
Premere un tasto per continuare . . .

Test this and when is ok you can remove the "echo" and left "ren" command or copy the output in a file and then execute.

Einstein1969
  • 317
  • 1
  • 13