Add text to end of filename (but before extension) using batch file

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

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

ren *.?* ????????????????????????????????????????????????????????" version 1".*
3

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"

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

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)

@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

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...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like