Rename multiple files without parentheses/remove parentheses windows
Asked Answered
S

4

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.

Sethrida answered 18/12, 2012 at 13:21 Comment(0)
C
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.

Canakin answered 18/12, 2012 at 17:4 Comment(8)
+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 !.Wilkerson
This solution will strip all parens. That could be a problem if there are additional parens before the underscore.Wilkerson
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 :)Canakin
how to rename all files in a folder without bracket?Peanut
Great solution, worked like a char, the filenames are ending up with spaces in them, any idea how to get rid of the spaces? thanksFaustofaustus
@ApexFred Add this line under the two others : set f=!f:^ =!Heaps
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?Laburnum
@BaliC Oops, I was overlooking it (;^ω^) Thanks!Laburnum
G
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..].

Gossamer answered 24/11, 2015 at 6:19 Comment(3)
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.Aphonia
Tried this (Maddins comment) on a rendered frames (blender) and the order got messed up. Just a word of cation. This does relabel the files, but the order will be messed up, I don't know power sell cmds that well but this would work great if it kept the order in which they were created. i.e. if @Aphonia knows how to accomplish, maybe add or rewrite cmd to accomplish this.Blew
@Blew The files are renamed in the order provided by Dir. I did a quick check and found that PS sorts files starting with _ (and probably others) at the end, as opposed to File Explorer. So first look at the output of Dir and adjust if needed, by adding sort conditions like Dir | Sort-Object -Property Name. When you've got the desired order, use it $nr=1; Dir | Sort-Object -Property Name | %{Rename-Item $_ -NewName ("test_{0:000}$($_.Extension)" -f $nr++)}Aphonia
M
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.

Magnesium answered 1/8, 2018 at 16:31 Comment(0)
L
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"
)
Longplaying answered 21/11, 2019 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.