user1788736
user1788736

Reputation: 2845

How to add diffrent prefix to file names in a folder using Windows command line (reading prefix names from textfile)?

I got a huge list of images in a folder (3000 images). Now I want to add a prefix to all these image names. The prefix for each image is different and the prefix is obtained from a text file.

My text file looks like this

mango,A.jpg
apple,B.jpg
orange,c.jpg

and i want to change image names fromA.jpg,B.jpg,C.jpg to:

mango_A.jpg
apple_B.jpg
orange_C.jpg

Could any one tell me how this can be done using a windows batch file?

Upvotes: 0

Views: 504

Answers (2)

foxidrive
foxidrive

Reputation: 41234

This is based on Mona's suggestion but is a little more robust, even if you have finished the task.

The prefix should not contain a comma, else the first comma will be where the prefix is taken up to.

Place this batch file and list.txt in the folder with your files and launch the batch file.

As it stands it will echo each rename command and pause for a keypress, for you to verify that it is doing what you need to do.

Remove the echo and pause to make it functional.

@echo off
for /f "tokens=1,* delims=," %%a in (list.txt) do (
   if exist "%%b" echo ren "%%b" "%%a_%%b"
   pause
)

Upvotes: 0

Monacraft
Monacraft

Reputation: 6630

Easy, try this:

pushd C:\..[Folder Path]
for /f "tokens=1,2 delims=," %%a in (list.txt) do (
ren "%%~b" "%%~a_%%~b"
)

And you're done!

Upvotes: 1

Related Questions