Solved Batch rename to remove (all text in brackets) 400K filenames

August 19, 2014 at 05:50:59
Specs: Windows 7, intel 3.3ghz 8gbs
I'm trying to rename approx 400,000 file names to remove all the text in brackets, all the available software just freezes when presented with the task.

Example_Filename (with random text in brackets).ext needs to become: Example_Filename.ext

Can anyone write me the code for a batch file to do this? All the files are in one folder, text in brackets can appear anywhere in the filenames, no files only have text in brackets.
Thanks.


See More: Batch rename to remove (all text in brackets) 400K filenames

Report •

✔ Best Answer
August 25, 2014 at 16:21:21
my bad! sorry, I fixed everthing in this one:

@echo off & setlocal enabledelayedexpansion
cd Here your folder name
set odd=1,3,5,7,9,11,13
set evn=2,4,6,8,10,12,14
:: ignore any files with no parens
for /f "tokens=*" %%a in ('dir /b *(*.txt') do (
set x=%%a
:: is ths really necessary? or "overkill". Attempts to hande files beginning with (
set t=!x:~0,1!
set x=!x:^)^(=!
if "!t!" equ "(" (
for /F "tokens=%evn% delims=()" %%b in ("!x!") do ren "%%a" "%%b%%c%%d%%e%%f%%g%%h"
) else (
for /F "tokens=%odd% delims=()" %%b in ("!x!") do ren "%%a" "%%b%%c%%d%%e%%f%%g%%h"
)
)
::======== end batch
fully tested.



#1
August 19, 2014 at 10:32:56
@echo off
cd Here your folder name
for /F "tokens=1-3 delims=()" %%a in ('dir /B /A-D') do ren "%%a(%%b)%%c" "%%a%%c"

Beware the script is not tested, so before a massive renaming test it and anyway get a full folder backup.

message edited by IVO


Report •

#2
August 19, 2014 at 14:06:31
Powershell, if you want to go that route:
dir "c:\some\path" | ForEach-Object {
  $str = [IO.Path]::GetFileNameWithoutExtension($_.Name)
  $sb = New-Object Text.StringBuilder ($str.Length)
  [int]$parCnt = 0  
  foreach ($c in $str.ToCharArray()) {
    if ($c -eq '(') { $parCnt++ ; if ($sb.Length -and ($sb[$sb.Length -1] -eq ' ')) { $sb.Remove($sb.Length - 1, 1) | Out-Null }}
    if (!$parCnt)   { $sb.Append($c) | Out-Null }
    if ($c -eq ')') { $parCnt-- }
  }
  $_ | Rename-Item -NewName ($sb.ToString().Trim() + $_.Extension) -PassThru
}

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#3
August 19, 2014 at 14:10:24
@ IVO. That did the majority of them, some of them have 2 or 3 bits of text in brackets: Example_Filename (part1)(V2.0)(ext).ext it doesn't do anything to those, can it be adapted to tackle them as well?
Thanks

message edited by Splodgeness


Report •

Related Solutions

#4
August 19, 2014 at 20:37:41
@echo off & setlocal enabledelayedexpansion
cd Here your folder name
set odd=1,3,5,7,9,11,13
set evn=2,4,6,8,10.12,14
for %%a in (*.*) do (
set x=%%a
set t=!x:~0,1!
if "!t!" equ "(" (set q=%evn%) else (set q=%odd%)
set x=!x:)(=!
for /F "tokens=!q! delims=()" %%b in ("!x!") do echo ren "!x!" "%%b%%c%%d%%e%%f%%g%%h"
)
::======== might work, depending on a lot of ASSumptions...
edited to fix a couple of the more glaring ones...

message edited by nbrane


Report •

#5
August 21, 2014 at 14:20:08
Unfortunately that's not even removing the single instances of text in brackets.

Report •

#6
August 21, 2014 at 19:51:17
I left safety ON:
for /F "tokens=!q! delims=()" %%b in ("!x!") do echo ren "!x!" "%%b%%c%%d%%e%%f%%g%%h"

safety OFF (remove 'echo'):
for /F "tokens=!q! delims=()" %%b in ("!x!") do ren "!x!" "%%b%%c%%d%%e%%f%%g%%h"

Not for sure, but give it a try.


Report •

#7
August 23, 2014 at 03:31:42
Even with the safety off I'm still getting nothing.

Try it out, create a text file called "New_File - [test] (blah) (BLAH!)(txt).txt"

That should cover all the possible variations in the file names. I just need to remove the text in brackets (not the square ones) so the result should be
"New_File - [test].txt"
Thanks

message edited by Splodgeness


Report •

#8
August 23, 2014 at 20:41:31
Well, the extra spaces will still be there, but here's this redo:

@echo off & setlocal enabledelayedexpansion
cd Here your folder name
set odd=1,3,5,7,9,11,13
set evn=2,4,6,8,10.12,14
:: ignore any files with no parens
for /f "tokens=*" %%a in ('dir /b *.txt ^| find "("') do (
set x=%%a
:: is ths really necessary? or "overkill". Attempts to hande files beginning with (
set t=!x:~0,1!
set x=!x:^)^(=!
if "!t!" equ "(" (
for /F "tokens=%evn% delims=()" %%b in ("!x!") do echo ren "!x!" "%%b%%c%%d%%e%%f%%g%%h"
) else (
for /F "tokens=%odd% delims=()" %%b in ("!x!") do echo ren "!x!" "%%b%%c%%d%%e%%f%%g%%h"
)
)
::======== end batch
I doubt if the leading parenthesis occurs, in which case the code can be trimmed quite a bit. But I went ahead and coded for it, considering the insanity of file-naming these days. Tested on your sample file and seemed to work ok. If the junk spaces are a problem, we'll go from there (cross that bridge and all that...)

message edited by nbrane


Report •

#9
August 24, 2014 at 01:57:53
Spaces are no problem. There are only 6 files that start with "(" so I removed them manually so I dont need that part.
When I run the batch now I get:

'find' is not recognized as an internal or external command, operable program or batch file.


Report •

#10
August 24, 2014 at 11:35:30
That's strange. FIND.EXE, on my win-7, dwells respectably in my windows\system32, which is of course included in my default path setting (see: SET PATH result). You have lots of options: 1) search for find.exe on your system:
cd \
dir /s find.exe
If it finds it, move or copy it into your default-path-chain or somewhere the batch can find it Or, copy it from another system that does have it.
2) use FINDSTR instead of find, assuming that FINDSTR is present and accounted for. Line 6 to: for /f "tokens=*" %%a in ('dir /b *.txt ^| findstr /c:"("') do (
3) change line 6 to use DIR instead of FIND. From:
for /f "tokens=*" %%a in ('dir /b *.txt ^| find "("') do (
to:
for /f "tokens=*" %%a in ('dir /b *(*.txt') do (

I wasn't totally convinced of DIR's ability to apply the expression correctly, but I think it should work that way (#3). No harm in trying it. Let me know. I'm curious as to why FIND is among the missing...


Report •

#11
August 25, 2014 at 02:24:40
I'd already checked to make sure FIND.EXE was in the right path. Turned out one of my other programs had wrongly changed the default path to it's folder, when I reset it to C:\Windows\System32 and ran the batch again I now get this (same with the other 2 variations you suggested):

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
G:\>cd G:\testfolder

G:\testfolder>set odd=1,3,5,7,9,11,13

G:\testfolder>set evn=2,4,6,8,10.12,14

G:\testfolder>for /F "tokens=*" %a in ('dir /b *.txt | find "("') do (
set x=%a
set t=!x:~0,1!
set x=!x:)(=!
if "!t!" EQU "(" (for /F "tokens=2,4,6,8,10.12,14 delims=()" %b in ("!x!") do r
en "!x!" "%b%c%d%e%f%g%h" ) else (for /F "tokens=1,3,5,7,9,11,13 delims=()" %b
in ("!x!") do ren "!x!" "%b%c%d%e%f%g%h" )
)

G:\testfolder>(
set x=New_File - [test] (blah) (BLAH!)(txt).txt
set t=!x:~0,1!
set x=!x:)(=!
if "!t!" EQU "(" (for /F "tokens=2,4,6,8,10.12,14 delims=()" %b in ("!x!") do r
en "!x!" "%b%c%d%e%f%g%h" ) else (for /F "tokens=1,3,5,7,9,11,13 delims=()" %b
in ("!x!") do ren "!x!" "%b%c%d%e%f%g%h" )
)

G:\testfolder>ren "!x!" "New_File - [test] .txt"
The system cannot find the file specified.

G:\testfolder>pause
Press any key to continue . . .
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Please note: The files are not all just txt files, there are many different file types.


Report •

#12
August 25, 2014 at 05:02:44
Ya know, #2's a perfectly valid solution.

How To Ask Questions The Smart Way


Report •

#13
August 25, 2014 at 09:22:55
Thanks Razor, My apologies for ignoring it, I have zero experience with powershell, I wouldn't know what to do with it and didn't want to get into a whole thing, but if you can explain exactly what I do with it in total noob speak then I'll give it a go.

Report •

#14
August 25, 2014 at 10:54:11
Replace "c:\some\path" on the first line with the full path to the file folder, then copy/paste it into Powershell or Powershell ISE.

How To Ask Questions The Smart Way


Report •

#15
August 25, 2014 at 16:21:21
✔ Best Answer
my bad! sorry, I fixed everthing in this one:

@echo off & setlocal enabledelayedexpansion
cd Here your folder name
set odd=1,3,5,7,9,11,13
set evn=2,4,6,8,10,12,14
:: ignore any files with no parens
for /f "tokens=*" %%a in ('dir /b *(*.txt') do (
set x=%%a
:: is ths really necessary? or "overkill". Attempts to hande files beginning with (
set t=!x:~0,1!
set x=!x:^)^(=!
if "!t!" equ "(" (
for /F "tokens=%evn% delims=()" %%b in ("!x!") do ren "%%a" "%%b%%c%%d%%e%%f%%g%%h"
) else (
for /F "tokens=%odd% delims=()" %%b in ("!x!") do ren "%%a" "%%b%%c%%d%%e%%f%%g%%h"
)
)
::======== end batch
fully tested.


Report •

#16
January 4, 2015 at 04:20:09
I suggest to try KrojamSoft BatchRenameFiles program. It’s really work for me.

Report •

Ask Question