1

Just trying to change a bunch of files that begin with

02. SongName.Mp3
02 - OtherName.Mp3
02 MoreNames.Mp3
03. Song.mp3
03 - Songs.mp3

to

SongName.Mp3
OtherName.Mp3
MoreNames.Mp3
Song.mp3
Songs.mp3

I know it's a really simple [ren] command, but it's escaping the tip of my brain. I'm using file>open>powershell on the folder, so no need to use dir or cd or anything.

mklement0
  • 382,024
  • 64
  • 607
  • 775

3 Answers3

1
Get-ChildItem *.mp3 | Rename-Item -NewName { $_.Name -replace '^[^\p{L}]+' } -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

The above renames *.mp3 files by removing any characters before the first letter (\p{L}) from the file name; files whose name already starts with a letter are left untouched.

Techniques used:

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

I'll take a crack at it. Replace numbers, dots, dashes, and spaces with nothing, assuming all files are mp3's. (Or "copy-item -destination")

# test files
# echo hi | set-content '02. SongName.Mp3','02 - OtherName.Mp3','02 MoreNames.Mp3','03. Song.mp3','03 - Songs.mp3'

dir *.mp3 | rename-item -newname { ($_.basename -replace '[\d\.\- ]') + '.mp3' } -whatif

What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02 - OtherName.Mp3 Destination: /Users/js/foo/OtherName.mp3".
What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02 MoreNames.Mp3 Destination: /Users/js/foo/MoreNames.mp3".
What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/02. SongName.Mp3 Destination: /Users/js/foo/SongName.mp3".
What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/03 - Songs.mp3 Destination: /Users/js/foo/Songs.mp3".
What if: Performing the operation "Rename File" on target "Item: /Users/js/foo/03. Song.mp3 Destination: /Users/js/foo/Song.mp3".

js2010
  • 23,033
  • 6
  • 64
  • 66
-1
   Get-ChildItem *.mp3 |
     Where-Object Name -match '^.{4}(.+).\.(.+)$' | 
       Copy-Item -Destination { $Matches.1 + '.' + $Matches.2 }

This works for what I was trying to do. Special note, it copies the files, it doesn't rename the existing ones.

  • This doesn't work correctly with `'02 MoreNames.Mp3'`, for instance; it's better not to rely on fixed character positions and match by character _type_ instead, as show in my answer. – mklement0 Feb 11 '20 at 03:22
  • It wasn't I who down-voted, but for the reason stated I do think your solution is suboptimal (and it can also be made more efficient by using a delay-bind script block with `Copy-Item` instead of a `Where-Object` call). That said, even I don't recommend it in this case, you may still accept your own answer 48 hours after posting it. Allow me to give you the standard advice to newcomers in the next comment. – mklement0 Feb 11 '20 at 09:59