14

I want to rename a large number of files in increasing order of numbers, starting from anywhere. But when I rename multiple files, it leaves me with parentheses. eg i rename files to abc_.jpeg it results in abc_(1).jpeg, abc_(2).jpeg and so on.

I tried using command prompt to rename

ren abc_(*).jpeg abc_*.jpeg
doesn't work. probably because of brackets

ren abc_"("*")".jpeg abc_*.jpeg
renames the files, but results in the same file name as before. I just want to remove the parentheses somehow.

neeraj
  • 1,191
  • 4
  • 19
  • 47

4 Answers4

17

To remove the brackets you will have to do some string manipulation. I have written a batch file to do this (save as .bat)

cd C:\folder
setlocal enabledelayedexpansion
for %%a in (abc_*.jpeg) do (
set f=%%a
set f=!f:^(=!
set f=!f:^)=!
ren "%%a" "!f!"
)

I don't think you can easily do this in one line from the command line though, it may be possible but it won't be pretty. If you can help it use this batch file to remove the brackets.

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • +1, but you should have quotes around the source and target names in case of space or other special chars. Also delayed expansion toggling will be needed if name can contain `!`. – dbenham Dec 18 '12 at 19:16
  • 1
    This solution will strip all parens. That could be a problem if there are additional parens before the underscore. – dbenham Dec 18 '12 at 20:10
  • 1
    Thanks. I guess so, but I am just answering based on the requirements asked in the OP. If it gets more complex I will leave it to you :) – Bali C Dec 18 '12 at 21:59
  • how to rename all files in a folder without bracket? – Web Designer cum Promoter May 09 '13 at 10:57
  • Great solution, worked like a char, the filenames are ending up with spaces in them, any idea how to get rid of the spaces? thanks – Jay Nov 19 '17 at 10:59
  • 1
    @ApexFred Add this line under the two others : set f=!f:^ =! – Gam Feb 25 '19 at 10:57
  • Hi, thank you for the batch file! It worked for me but the new file names have a blank space (for example, "abc 1.jpg", "abc 2.jpg", ...) . Is there any way to avoid this? – Ogiad Sep 23 '21 at 15:35
  • 1
    @BaliC Oops, I was overlooking it (;^ω^) Thanks! – Ogiad Sep 24 '21 at 15:55
12

In the File Explorer window, select all files, right-click and select rename. Windows will select the starting number as the number supplied between the round brackets so name the file using a number that is 1 digit more than the number of digits required.

Example: We want the pattern "test_xxx". Using the File Explorer, rename the files to "tes(1000)". Your files will now be named ["tes(1000)", "tes(1001)", "tes(1002)", etc..]. Hold SHIFT and right click in the open area of the File Explorer, then choose "Open command window here". Issue the following command:

ren *.* test_???.*

This will rename all the files to the proper format ["test_000", "test_001", "test_002", etc..].

Subskybox
  • 657
  • 1
  • 7
  • 6
  • 5
    Great solution, because it's a one liner. On Windows 10 though, "_Open command window here_" has been replaced by "_Open PowerShell here_". To get the same with powershell use `$nr=1; Dir | %{Rename-Item $_ -NewName ("test_{0:000}$($_.Extension)" -f $nr++)}` with `$n=1` initializing the counter with 1. To start with higher index use `$n=100` instead, for example. – Maddin Apr 23 '20 at 10:54
7

A bit late to the party, but here's a combination of removing parentheses and the empty space automatically created. This code works by having the .bat file inside a folder containing all the files you'd like to modify.

Copy and paste the code in notepad and save it as sequentialFileNameCleaner.bat

Your file name must be the same as what is written on the first line sequentialFileNameCleaner.bat. That being said, you can manually update the first line if you want to change the file name.

:sequentialFileNameCleaner  [/R]  [FolderPath]
setlocal enabledelayedexpansion
for %%a in (*.jpg) do (
set f=%%a
set f=!f:^(=!
set f=!f:^)=!
ren "%%a" "!f!"
)
@echo off
setlocal disableDelayedExpansion
if /i "%~1"=="/R" (
  set "forOption=%~1 %2"
  set "inPath="
) else (
  set "forOption="
  if "%~1" neq "" (set "inPath=%~1\") else set "inPath="
)
for %forOption% %%F in ("%inPath%* *") do (
  if /i "%~f0" neq "%%~fF" (
    set "folder=%%~dpF"
    set "file=%%~nxF"
    setlocal enableDelayedExpansion
    echo ren "!folder!!file!" "!file: =!"
    ren "!folder!!file!" "!file: =!"
    endlocal
  )

)

By default, this code will only locate .jpg files. On the 3rd line, changing the (*.jpg) to (*.png) or to (*.mp4) or any extension you'd like will make the code compatible.

SlayerCat
  • 146
  • 2
  • 6
0

The problem comes when we need to pass a list of specific parenthesis-containing file names to the script. The following does work for this. In this example, we are changing parentheses to underscores.

SET fileList=%*
SET delim1=aaaaaaaaaaaaa
SET delim2=zzzzzzzzzzzzz
setlocal enabledelayedexpansion
SET fileList=!fileList:^(=%delim1%!
SET fileList=!fileList:^)=%delim2%!
FOR %%f in (%fileList%) DO (
 SET f1=%%~f
 SET f1=!f1:%delim1%=^(!
 SET f1=!f1:%delim2%=^)!
 SET f2=%%f
 SET f2=!f2:%delim1%=_!
 SET f2=!f2:%delim2%=_!
 FOR %%i IN (!f2!) DO RENAME "!f1!" "%%~nxi"
)
Michael Weiner
  • 109
  • 1
  • 3