-2

I've a set of image files, which I need to renamed in a specific format. Currently they were like:

img (1).jpg
img (2).jpg
img (3).jpg
img (4).jpg
img (5).jpg
...

But I need to rename them as:

img_01.jpg
img_02.jpg
img_03.jpg
img_04.jpg
img_015.jpg
...

Any idea how to rename it in simple trick instead of manually going through one by one? Much appreciate your help!

Thank You!

Althaf
  • 143
  • 2
  • 11
  • 1
    https://stackoverflow.com/questions/3808001/how-do-i-create-batch-file-to-rename-large-number-of-files-in-a-folder – avery_larry May 13 '20 at 18:58
  • 1
    https://stackoverflow.com/questions/9383032/rename-all-files-in-a-directory-with-a-windows-batch-script – avery_larry May 13 '20 at 18:59
  • I can only assume that your example has a typo, `img (5).jpg` → `img_015.jpg`. If not please explain the criteria for the renaming. Regardless of that, are you sure that you're intending to make that particular change? it would make more sense from a sorting perspective if your numeric part has the same number of digits, i.e. `img (1).jpg` → `img_001.jpg`, `img (12).jpg` → `img_012.jpg`, `img (123).jpg` → `img_123.jpg` etc. As for a simple trick, I'm afraid there's no special command to do it. What you'll need to do is capture each name, split and/or replace each as necessary, then rename. – Compo May 13 '20 at 19:51

1 Answers1

2

I've solved such situations frequently in the past, I did it the following way: instead of writing a for-loop with some fancy variable and string handling, I used a basic text editor with column mode (like Notepad++), and I made a list of files I wanted to rename:

img (1).jpg
img (2).jpg
img (3).jpg
img (4).jpg

Using the column mode, I copied the same column next to it, so I get:

img (1).jpg img (1).jpg
img (2).jpg img (2).jpg
img (3).jpg img (3).jpg
img (4).jpg img (4).jpg

Using a basic "Replace", I then altered the second column to what I wanted (replace " (" by "_0" and remove the closing brackets):

img (1).jpg img_01.jpg
img (2).jpg img_02.jpg
img (3).jpg img_03.jpg
img (4).jpg img_04.jpg

Again using the column mode, I added move at the beginning of each line:

move "img (1).jpg" img_01.jpg
move "img (2).jpg" img_02.jpg
move "img (3).jpg" img_03.jpg
move "img (4).jpg" img_04.jpg

I put this into a batchfile, I tested some random lines in order to check if everything was done fine, and then I launched the whole thing.

Obviously, if you need to do this regularly, it might be better to have a more automated way of working, but if this is a "once-or-twice-in-a-lifetime", do it like this.

Dominique
  • 16,450
  • 15
  • 56
  • 112