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

Solution 1:

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

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

Solution 2:

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"

Solution 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

Solution 4:

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)