Home > Mobile >  How to remove a random string in-between two square brackets from a file name
How to remove a random string in-between two square brackets from a file name

Time:01-08

I was editing a .bat script to rename files after torrenting and all files have a random string in between two square brackets e.g.

Episode 13 [EA2614CB].mkv

I don't have much coding knowledge and only occasionally use PowerShell scripts for fun and to automate a task I'm too lazy to do.

Here's the script

@echo off

REM This changes the drive to the one with my downloads
B:

REM This moves the directory to the final download location
cd "\Anime\.Purgatory"

REM These commands will automatically rename any mp4 or mkv files in the download. It will get rid of the usual download tags. 
REM If you want to add more tags, copy both lines if you want to include mp4 and mkv, then change the name between the 1st ''
REM for example '1080p', then add what you want that to change to in the second set of ''
REM for ecample '' for nothing or ' ' for a space, or 'EXAMPLE TEXT'
REM As the list grows, the batch file will take longer to run through

REM Download Tags
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('(1080p)','').replace('[SubsPlease]','')   $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('(1080p)','').replace('[SubsPlease]','')   $_.Extension }"


REM Test
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName -replace '\[','(' -replace '\]',')' -replace '[[\]]','' -replace '\(. \)','' } , $_.Extension"


REM Season Tags
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E').replace('.S02E',' S02 E').replace('.S03E',' S03 E').replace('.S04E',' S04 E').replace('.S05E',' S05 E').replace('.S06E',' S06 E').replace('.S07E',' S07 E').replace('.S08E',' S08 E').replace('.S09E',' S09 E').replace('.S10E',' S10 E').replace('.S11E',' S11 E').replace('.S12E',' S12 E').replace('.S13E',' S13 E').replace('.S14E',' S14 E').replace('.S15E',' S15 E')   $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E').replace('.S02E',' S02 E').replace('.S03E',' S03 E').replace('.S04E',' S04 E').replace('.S05E',' S05 E').replace('.S06E',' S06 E').replace('.S07E',' S07 E').replace('.S08E',' S08 E').replace('.S09E',' S09 E').replace('.S10E',' S10 E').replace('.S11E',' S11 E').replace('.S12E',' S12 E').replace('.S13E',' S13 E').replace('.S14E',' S14 E').replace('.S15E',' S15 E')   $_.Extension }"


REM Clean Up
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('  ',' ').replace('   ',' ')   $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('  ',' ').replace('   ',' ')   $_.Extension }"

REM This command will then move the newly named mp4 or mkv files to their final location. This works the same as cut paste. NOT copy and paste.
Robocopy "B:\Anime\.Purgatory" B:\Anime\.Heaven /s /mov

pause

The issue is in the REM Test section where the random string is removed, I managed to change the square brackets to normal brackets and use code I found to delete the normal brackets and everything in them but this means I have to use the -replace instead of .replace.

This only works to the point where I have to add the extension back at the end, and then I get an error. If I don't add the extension everything works but now the file no longer has an ending and isn't playable.

I'm not sure what to do any help is appreciated.

CodePudding user response:

Use regexr

  • Related