Rename multiple files in cmd
Asked Answered
S

10

39

If I have multiple files in a directory and want to append something to their filenames, but not to the extension, how would I do this?

I have tried the following, with test files file1.txt and file2.txt:

ren *.txt *1.1.txt

This renames the files to file1.1.txt and file2.txt1.1.txt

I want the files to be file1 1.1.txt and file2 1.1.txt

Will this be possible from cmd or do I need to have a bat file to do this? What about PowerShell?

Subterranean answered 24/6, 2013 at 8:57 Comment(4)
are there a lot of files?Pathological
Theres about 90 in a folderSubterranean
You can also do this with just the UI if you want. Select all the files, rename one to "file" and it will show up as file (1), file (2), file(3), file (4), etc.Vitovitoria
@Morne, You can also use a tool like Massive File Renamer superuser.com/a/730292/78897Egidio
G
22
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"

If you use the simple for loop without the /f parameter, already renamed files will be again renamed.

Graber answered 24/6, 2013 at 11:21 Comment(2)
what '~n' in "%%~ni 1.1%%~xi" stands for? i dont understand how it work...Ka
Read the for documentation, it explains how variable placeholders work.Hulett
P
31

Make sure that there are more ? than there are characters in the longest name:

ren *.txt "???????????????????????????? 1.1.txt"

See How does the Windows RENAME command interpret wildcards? for more info.

New Solution - 2014/12/01

For those who like regular expressions, there is JREN.BAT - a hybrid JScript/batch command line utility that will run on any version of Windows from XP forward.

jren "^.*(?=\.)" "$& 1.1" /fm "*.txt"

or

jren "^(.*)(\.txt)$" "$1 1.1$2" /i
Pyrometer answered 24/6, 2013 at 15:36 Comment(3)
The absolute max file name length is 255 characters. But the practical limit for most scenarios is less, since the max total path lengh supported by the Windows UI is 259 characters. The total path length includes volume, folder, and file name information, plus a null terminator. See msdn.microsoft.com/en-us/library/aa365247.aspx for more info.Pyrometer
Thanks for introducing jren, it was exactly what I needed, simple short and fast. Btw, with it it's possible to use shorthand character classes like \d or \w.Presentationism
@Presentationism - Yes - JREN uses standard JSCRIPT regex, so the shorthand character classes are supported. You can read the Microsoft regex syntax documentation .Pyrometer
E
24

Step 1:

Select all files (ctrl + A)

Step 2 :

Then choose rename option

enter image description here

Step 3:

Choose your filename... for ex: myfile

it automatically rename to myfile (01),myfile (02),,.....

If you want to replace spaces & bracket.. continue step 4

Step 4:

Open Windows Powershell from your current folder

enter image description here

Step 5:

For replace empty space to underscore (_)

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape(" "),"_"}

Step 6:

For replace open bracket

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape("("),""}

For replace close bracket

 dir | rename-item -NewName {$_.name -replace [Regex]::Escape(")"),""}
Elo answered 17/11, 2016 at 14:27 Comment(4)
This powershell is great. ThanksConstantin
Credits perhaps to howtogeek.com/111859/… ?Iatry
I did the same thing for renaming all images and videos in a folder. But when filetype changes the number again starts from 1.Boarish
Could you please check this question of mine #77478781Boarish
G
22
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"

If you use the simple for loop without the /f parameter, already renamed files will be again renamed.

Graber answered 24/6, 2013 at 11:21 Comment(2)
what '~n' in "%%~ni 1.1%%~xi" stands for? i dont understand how it work...Ka
Read the for documentation, it explains how variable placeholders work.Hulett
S
13

The below command would do the job.

forfiles /M *.txt /C "cmd /c rename @file \"@fname 1.1.txt\""

source: Rename file extensions in bulk

Saltpeter answered 9/1, 2015 at 7:10 Comment(1)
+1 I think this is the easiest one to remember, much simpler than the for syntax of cmd, but the letdown is that it has to spawn a new cmd.exe process for every iteration. That makes this very inefficient and slow as molasses.Tryparsamide
P
4

Try this Bulk Rename Utility It works well. Maybe not reinvent the wheel. If you don't necessary need a script this is a good way to go.

Pathological answered 24/6, 2013 at 9:5 Comment(1)
still a good solution if you do not want to .... scriptRubicon
J
4
@echo off
for %%f in (*.txt) do (
    ren "%%~nf%%~xf" "%%~nf 1.1%%~xf"
)
Jackal answered 24/6, 2013 at 9:47 Comment(1)
For someone will not run : for %f in (*.txt) do (ren "%~nf%~xf" "%~nf 1.1%~xf")Kiki
Y
2

I found the following in a small comment in Supperuser.com:

@JacksOnF1re - New information/technique added to my answer. You can actually delete your Copy of prefix using an obscure forward slash technique: ren "Copy of .txt" "////////"

Of How does the Windows RENAME command interpret wildcards? See in this thread, the answer of dbenham.

My problem was slightly different, I wanted to add a Prefix to the file and remove from the beginning what I don't need. In my case I had several hundred of enumerated files such as:

SKMBT_C36019101512510_001.jpg
SKMBT_C36019101512510_002.jpg
SKMBT_C36019101512510_003.jpg
SKMBT_C36019101512510_004.jpg
:
:

Now I wanted to respectively rename them all to (Album 07 picture #):

A07_P001.jpg
A07_P002.jpg
A07_P003.jpg
A07_P004.jpg
:
:

I did it with a single command line and it worked like charm:

ren "SKMBT_C36019101512510_*.*" "/////////////////A06_P*.*"

Note:

  1. Quoting (") the "<Name Scheme>" is not an option, it does not work otherwise, in our example: "SKMBT_C36019101512510_*.*" and "/////////////////A06_P*.*" were quoted.
  2. I had to exactly count the number of characters I want to remove and leave space for my new characters: The A06_P actually replaced 2510_ and the SKMBT_C3601910151 was removed, by using exactly the number of slashes ///////////////// (17 characters).
  3. I recommend copying your files (making a backup), before applying the above.
Yepez answered 1/12, 2019 at 7:37 Comment(0)
O
1

I tried pasting Endoro's command (Thanks Endoro) directly into the command prompt to add a prefix to files but encountered an error. Solution was to reduce %% to %, so:

for /f "delims=" %i in ('dir /b /a-d *.*') do ren "%~i" "Service.Enviro.%~ni%~xi"
Ointment answered 31/3, 2015 at 22:48 Comment(0)
J
0

I was puzzled by this also... didn't like the parentheses that windows puts in when you rename in bulk. In my research I decided to write a script with PowerShell instead. Super easy and worked like a charm. Now I can use it whenever I need to batch process file renaming... which is frequent. I take hundreds of photos and the camera names them IMG1234.JPG etc...

Here is the script I wrote:

# filename: bulk_file_rename.ps1
# by: subcan
# PowerShell script to rename multiple files within a folder to a 
# name that increments without (#)
# create counter
$int = 1
# ask user for what they want
$regex = Read-Host "Regex for files you are looking for? ex. IMG*.JPG "
$file_name = Read-Host "What is new file name, without extension? ex. New Image "
$extension = Read-Host "What extension do you want? ex. .JPG "
# get a total count of the files that meet regex
$total = Get-ChildItem -Filter $regex | measure
# while loop to rename all files with new name
while ($int -le $total.Count)
{
    # diplay where in loop you are
    Write-Host "within while loop" $int  
    # create variable for concatinated new name - 
    # $int.ToString(000) ensures 3 digit number 001, 010, etc
    $new_name = $file_name + $int.ToString(000)+$extension
    # get the first occurance and rename
    Get-ChildItem -Filter $regex | select -First 1 | Rename-Item -NewName $new_name
    # display renamed file name
    Write-Host "Renamed to" $new_name
    # increment counter
    $int++
}

I hope that this is helpful to someone out there.

subcan

Janellajanelle answered 17/4, 2014 at 22:37 Comment(0)
U
0

This works for your specific case:

ren file?.txt "file? 1.1.txt" 
Underlie answered 6/3, 2018 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.