26

I have a list of files which are in the format on the left. I want to append a string to each file name, but before the extension, as shown:

File 1.txt  ->  File 1 version 1.txt
File 2.txt  ->  File 2 version 1.txt
File 3.txt  ->  File 3 version 1.txt
File 4.txt  ->  File 4 version 1.txt
...

In each case, the appended string is the same across all files.

This seems like such a simple task, but I'm having some trouble putting this into a batch file.

I've tried the ren command and followed some the the examples on this page, but the resulting file names append the string after the extension:

File 1.txt version 1.txt
1

6 Answers 6

7

for longer filenames you will have to add some more ? in the block with the mass of ?????????

ren *.?* ????????????????????????????????????????????????????????" version 1".*
3
  • 2
    Thanks for answering so promptly! I edited the command slightly, to retain the file extension, and to only affect txt files: ren *.txt ?????????????????????????" Version 1.txt"
    – TheTurkey
    Jun 5, 2013 at 11:13
  • what if I want to append something at the beginning Aug 5, 2015 at 6:25
  • how many ? characters are needed? Feb 2, 2017 at 7:14
52

Instead of relying on ren's undocumented quirks, why not do this the proper way?

for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"

If you want to use this in a batch file just remember to double each % sign.

This line of code will loop through all the files matching *.txt and perform a rename command (ren) on each filename. %a is the full file name, %~a is the filename without extra quotes added (quotes are added in the command). %~na is the filename without the file extension, and %~xa is the file extension.

If there is a single file, File 1.txt in the current directory the following command will be executed: ren "File 1.txt" "File 1 version 1.txt"

5
  • 1
    Excellent! this is a better answer any day. +1
    – Fr0zenFyr
    Jul 24, 2014 at 10:05
  • Do you have more documentation about "%~a" and others so we can make our own version?
    – Jomme
    Jan 1, 2020 at 18:49
  • See this question for more information about "%~": stackoverflow.com/questions/3215501 Oct 8, 2021 at 15:32
  • This worked like a charm for batch-renaming files! Apologies if this is a super basic question (I'm very new to CMD), but how should this be modified to batch-rename folders instead of files?
    – Kinch
    May 1, 2023 at 19:54
  • Never mind, just found out about the /D switch from this other thread: superuser.com/questions/429752/…
    – Kinch
    May 1, 2023 at 20:08
3

I wanted to add "_A" to multiple files and this is how I did it.

I removed the extension first, added the suffix, then changed extension back to original. It sure did work for me and kept the batch code simple, as I am not much of a programmer.

@ECHO OFF

REN *.pdf *.
REN *. *_A.
REN *. *.pdf
0
2

Some help for Karan's answer:

%~I         - expands %I removing any surrounding quotes ("")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

Sorry, I can't answer to posts yet...

1

For the folks who are inexperienced with command (like me), there are a couple of softwares that make bulk rename very easy:

  • Bulk rename utility (no open source)
  • Rename-It! (open source)
1

@bwDraco's solution was good but it could have been kept simpler. My set of files were all *.jpg and I wanted to rename them all *c.jpg. You only need 2 steps for this, not 3:

ren "{filepath}\*.jpg" *.
ren "{filepath}\*" *c.jpg

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .