1

I have filenames in a directory that have different beginning strings of characters that I wish to strip out and leave just the alpha characters + file extension:

[example]

1-09. But Not for Me.mp3
1-11. Summertime.mp3
1-12. My Funny Valentine.mp3
13. Indiana (Back Home Again in Indiana).mp3
2-02 I Love You.mp3
2-05 You'd Be So Nice To Come Home To.mp3
2-11. A Foggy Day.mp3

The only pattern I see is that the RegEx should strip out all the characters up to the FIRST alpha character.

[desired output]

But Not for Me.mp3
Summertime.mp3
My Funny Valentine.mp3
Indiana (Back Home Again in Indiana).mp3
I Love You.mp3
You'd Be So Nice To Come Home To.mp3
A Foggy Day.mp3

What would the Powershell look like to accomplish this?

I thought something along these lines but I don't know RegEx well enough to accomplish my goal.

foreach ($file in (get-childitem *.mp3).name) { Rename-Item $file ($file -replace '^[^-]*-\s*') }

thanks

1
  • Check out regex101.com there you can test your regex
    – Turdie
    Commented Oct 28, 2023 at 20:56

1 Answer 1

0

use: ^\d+(?:-\d+)?\.? and replace with nothing.

Explanation:

^           # beginning of line
    \d+         # 1 or more digits
    (?:         # non capture group
        -           # hyphen
        \d+         # 1 or more digits
    )?          # end group, optional
    \.?         # optional dot
                # a space

Demo & Explanation

5
  • Hello, I tested that RegEx in Notepad++ it but it only selects the numerical portion of the line, not the characters up to the first Alpha character Commented Nov 1, 2023 at 16:27
  • @JeffTaylor: Have you put a space at the end of the regex? As it's describe in my answer. I also tested in Notepad++ and it works fine, at least for the examples given.
    – Toto
    Commented Nov 1, 2023 at 17:06
  • good catch. I did add one whitespace and that does pickup a few more lines but still not picking up a "-" or additional whitespace leading up to the first alpha character. Commented Nov 1, 2023 at 18:21
  • @JeffTaylor: Could you give some examples that don't work? Probably change the last space with [- ]*
    – Toto
    Commented Nov 1, 2023 at 19:01
  • Yes @Toto that worked perfectly. Thanks for hanging in there to help out. This was my final one-liner: foreach ($file in (get-childitem *.mp3).name) { Rename-Item $file ($file -replace '^\d+(?:-\d+)?\.?[- ]*')} Commented Nov 2, 2023 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .