3

I cant find a find an answer that is clear to me. I am changing a bunch of image file names in a directory, when I rename them they all get renamed to image (1).png image (2).png for as many images as I have then I run this in power shell.

Dir | Rename-Item –NewName { $_.name –replace " ","_" }

This finds the space and renames it to image_(1).png image_(2).png nice and easy, but It becomes a headache trying to replace the parentheses. I am trying to get rid of them to look like this image_1.png image_2.png but it's gotten really frustrating finding an answer lol.

I wish I could just write.

Dir | Rename-Item –NewName { $_.name –replace "\(*\)","*" } 

or

Dir | Rename-Item –NewName { $_.name –replace "\([1-10]\)","[1-10]" }

or

Dir | Rename-Item –NewName { $_.name –replace "\(\W\)","\W" }

I tried them all and you would think that syntax is valid, but nope :( So I am hoping for a little nudge in the right direction.

4 Answers 4

8

I don't have enough SO Rep to comment on your question so I have to try for an answer. Do you need to escape your backslashes? ie. Replace

"\(*\)","" with "\\(*\\)",""

Is your issue with the command syntax or the Regex itself? The raw Regex would look something like this:

^(?\<Name>.*)\((?<Number>\d*?)\)\.(?<Extension>.*$)

and replace with: ${Name}_${Number}.${Extension}

Sign up to request clarification or add additional context in comments.

9 Comments

I need to remove them, having () in file names aren't really flexiable when using in css and stuff. I am not 100% what the issue is, I know some syntax's are valid but make no changes, and them some I tried like @EBGreen's answer give me an error saying cannot rename.
I just updated my response to correct the formatting. The regex I posted will capture those and strip out the parens replacing them with the initial underscore. So: image (1).png becomes image _1.png
Okay I am trying it now, thanks so much for the answer.
Sure. Bear in mind, it's possible you'll have to use double backslashes when using my solution. I don't know if powershell requires you to escape those characters in strings or not, but I'd assume you probably would.
nice this Dir | Rename-Item –NewName { $_.name –replace "\(*\)","" } removed one side of the parentheses file name looks like this image_(1.png almost there but not sure why it didnt remove both?
|
5

How about:

Dir | Rename-Item –NewName { $_.name.replace(' ','_') -replace '[()]','' }

The string .replace() method is much more efficient for single literal character replacement. Use the -replace operator for more complex operations where you need to specify multiple characters at once.

Comments

2

Couldn't comment under the answer but this is a way to getting rid of the "()" and the text inside in one go.

Rename-Item –NewName { $_.name -replace "(\(\d+\))",""}

The \( and \) escapes the parentheses making them literal parentheses. The non-escaped parentheses group the captured pattern to be referenced later and may not be needed here for what you are doing.

Hope this helps someone.

Comments

0

I did with two lines
Dir | Rename-Item –NewName { $.name –replace "(","" } Dir | Rename-Item –NewName { $.name –replace ")","" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.