-4

I have a lot of files named like

Files_01(some text).png
Files_02(some different text).png
Files_03(some other text).png
Files_04(totally different text).png

What I'm looking for is a way to remove everything in the brackets so that I'm left with:

Files_01.png
Files_02.png
etc.

I tried the following, but it didn't remove the text in parentheses:

Get-ChildItem .png | foreach {
  Rename-Item $_ $_.Name.Replace("()", "")
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Aarron
  • 11
  • 2
  • Can you post what you have tried so far? – Eric S Nov 02 '16 at 21:07
  • have you looked at regular expressions on regex101.com to guide you? – Kory Gill Nov 02 '16 at 21:09
  • 2
    Welcome to Stack Overflow! We are a community to help programmers and programming enthusiasts. That being said, it is expect that you show what you have done or tried before posting. This gives us something to build on. As of now this reads like a code writing request which is off topic for SO. Break your question into its parts and search individually for solutions to those problems. Then, if you are still having issues, please [edit] your question showing your work so we can better help you and the community. – Matt Nov 02 '16 at 21:19
  • Simply open Emacs, enter a dired view of your directory, switch to writable mode, [search and replace](http://stackoverflow.com/a/15882553/478656), then finish editing mode. – TessellatingHeckler Nov 02 '16 at 21:29
  • I've not tried anything yet, I've no idea where to start really. I have tried looking at similar questions to mine but all I can find really are answer for changing a single string in a file name. I'm struggling to find something similar for removing different strings. Each file has a completely different name and there is hundreds of them. – Aarron Nov 02 '16 at 21:45

3 Answers3

4

This is how I completed the task. I read in the file objects from the directory, then check if the filename contains parentheses. If the name does, I replace the parentheses and any text in between with an empty string. Then call Rename-Item and pass it the new file name without the parentheses.

Get-ChildItem -Path C:\PowershellTest\ | % {if($_.Name -match '.*\(.*\).*') {Rename-Item -Path $_.FullName -NewName $($_.Name -replace '\(.*\)', '')}}
jdwal
  • 189
  • 6
0

Try like this :

@echo off
set "$ext=.png"

for /f "delims=" %%b in ('dir /b/a-d *%$ext%') do for /f "tokens=1 delims=()" %%c in ('echo %%b') do ren "%%~nb%$ext%" "%%~nc%$ext%"
SachaDee
  • 9,245
  • 3
  • 23
  • 33
0

In powershell I'd probably create something like this:

$myFolder = "C:\Users\Aarron\Pictures\"

Foreach ($file in Get-Childitem $myFolder\*.png)
{
    $ReName = $file.basename.split("(")[0] + $file.extension
    Rename-Item $file.FullName $ReName
}

…and a possible batch solution:

@Echo Off
Set "myFolder=C:\Users\Aarron\Pictures\"
For %%A In ("%myFolder%\*.png") Do Call :Sub "%%~dpA" "%%~nA" "%%~xA"
Exit/B
:Sub
Set "ReName=%~2"
Set ReName=%ReName:(=&:%
Ren "%~1%~2%~3" "%ReName%%~3"
Compo
  • 36,585
  • 5
  • 27
  • 39