lunatic84
lunatic84

Reputation: 329

How to remove text by regex for all files?

E.g my files are

-a----       10/05/2018     22:15       12550016 01. Eat the Elephant (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       14163840 02. Disillusioned (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15        9547648 03. The Contrarian (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       11272064 04. The Doomed (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       10649472 05. So Long, and Thanks for All the Fish (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15        9961344 06. TalkTalk (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       12201856 07. By and Down the River (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15        9183104 08. Delicious (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15        5074816 09. DLB (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       12578688 10. Hourglass (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       13938560 11. Feathers (2018_07_06 06_26_52 UTC).mp3
-a----       10/05/2018     22:15       16031616 12. Get the Lead Out (2018_07_06 06_26_52 UTC).mp3

I am tring to remove all text between brackets, the brackets, and the space before the first bracket from my files.

I tried with this script below in PowerShell using regex but it doesn't work as I expected.

Get-ChildItem -Recurse | Rename-Item -NewName {$_.Name.Replace(" \((.*?)\)", "")}

Upvotes: 1

Views: 607

Answers (2)

Yves Kipondo
Yves Kipondo

Reputation: 5603

Because You want to remove a specific part of each line I use a capturing parentheses which allow to get each part captured in the $Matches variables and because there are only one block of capturing parentheses I use the index 1 to access the match part in the $Matches variable.

And the result of the $Matches variable look like this

And the result of the $Matches variable look like this

Name                           Value                                                                                                 
----                           -----                                                                                                 
1                               (2018_07_06 06_26_52 UTC)                                                                            
0                              -a----       10/05/2018     22:15       13938560 11. Feathers (2018_07_06 06_26_52 UTC).mp3           

To perform the replace I check if there are in the $chain variable a part which match the regular expression pattern If It's the case I perform the replace

$chain = "-a----       10/05/2018     22:15       13938560 11. Feathers (2018_07_06 06_26_52 UTC).mp3"
if($chain -match '.+(\s\(.+\)).+'){
    $new_filename = $chain.Replace($Matches[1], "")
}

After replacing the $new_filename will contain

-a----       10/05/2018     22:15       13938560 11. Feathers.mp3

Because you are Getting list of files with Get-ChildItem -Recurse You can save the result in a variable and loop through the results and check if each element in the array has a maching part and perform a replacement

Upvotes: 0

pfx
pfx

Reputation: 23244

PowerShell's Replace operator accepts a regular expression.

Get-ChildItem -Recurse | Rename-Item -NewName { $_.Name -Replace " \((.*?)\)", "" }

See Rename-Item combined with the -Replace operator.

($_.Name.Replace is just doing a plain text/string replace.)

Upvotes: 1

Related Questions