I have several videos that has either round brackets and/or square brackets with some text inside those like (ANTYHING HERE) OR [ANTHING HERE]:
For example:
MOVIENAME 2024 LANG [SOMETEXTHERE] 1080P.mkv or MOVIENAME 2024 (SOMEOTHERTEXTHERE) LANG [SOMETEXTHERE] 1080P.mp4 or
And by using Powershell with Regex text replacing, I wanna rename it to MOVIENAME 2024 LANG 1080P.mkv or MOVIENAME 2024 LANG 1080P.mp4 means I wanna remove any round and/or square brackets and anything inside those with also removing any extra spaces, meaning find & convert 2 or more spaces to just 1 space, possibly in single -replace pass or couple such passes at most.
I tried doing this just for square brackets, but it doesn’t work:
$validExtensions = '.mkv', '.mp4'
Get-ChildItem -Recurse -File |
Where-Object Extension -In $validExtensions |
Where-Object Length -GT 500mb |
Rename-Item -NewName {($_.BaseName -replace '(.*)(\[.*\])(.*)', '$1$3')}.ToUpper() + $_.Extension
Can anyone help achieving what I want here ?
>Solution :
Chain two -replace operations, as follows:
($_.BaseName -replace '[[(].*[])]' -replace ' +', ' ').ToUpper() + $_.Extension
-
The first one removes any
[...]and(...)tokens from the string .- Strictly speaking, it would also remove tokens with mismatched delimiters, e.g.
[...) - If that is a concern, use the regex suggested by TheMadTechnician:
'\[.*?\]|\(.*?\)'
- Strictly speaking, it would also remove tokens with mismatched delimiters, e.g.
-
The second replaces runs of two or more spaces with a single space each.