35

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Morne
  • 1,623
  • 2
  • 18
  • 33

10 Answers10

30

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
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 4
    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 http://msdn.microsoft.com/en-us/library/aa365247.aspx for more info. – dbenham Jun 24 '13 at 19:47
  • 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](http://www.regular-expressions.info/shorthand.html) like `\d` or `\w`. – Armfoot May 22 '15 at 19:33
  • @Armfoot - Yes - JREN uses standard JSCRIPT regex, so the shorthand character classes are supported. You can read the [Microsoft regex syntax documentation](https://msdn.microsoft.com/en-us/library/ae5bf541) . – dbenham Sep 09 '15 at 13:37
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.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 1
    what '~n' in "%%~ni 1.1%%~xi" stands for? i dont understand how it work... – ocrampico Dec 10 '17 at 11:00
  • Read the [`for` documentation](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/for), it explains how variable placeholders work. – Remy Lebeau Nov 07 '18 at 02:30
22

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(")"),""}
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
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

Giri
  • 512
  • 5
  • 9
  • +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. – Amit Naidu Nov 03 '16 at 19:00
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.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
4
@echo off
for %%f in (*.txt) do (
    ren "%%~nf%%~xf" "%%~nf 1.1%%~xf"
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
Eugene
  • 3,335
  • 3
  • 36
  • 44
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.
Juv
  • 744
  • 7
  • 12
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"
MJA
  • 350
  • 1
  • 3
  • 15
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

user4317867
  • 2,397
  • 4
  • 31
  • 57
subcan
  • 2,021
  • 2
  • 18
  • 21
0

This works for your specific case:

ren file?.txt "file? 1.1.txt" 
jfajunior
  • 1,211
  • 1
  • 15
  • 19