Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename multiple files in a directory using batch script

I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...

for /r %%i in (*) do (
    echo %c% 
)
like image 835
Jack Mc Lauren Avatar asked Aug 30 '14 14:08

Jack Mc Lauren


People also ask

How do I batch rename multiple files at once?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

How do I batch rename files in CMD?

Highlight each file which you want to rename. Press Ctrl+A to highlight them all, if not, then press and hold Ctrl and click on each file you want to highlight. Once all the files are highlighted, right-click on the first file and click on “Rename” (press F2 to rename the file without making more actions).


2 Answers

Here's the script. Just put the script in your folder and run it.

@echo off & setlocal EnableDelayedExpansion 

set a=1
for /f "delims=" %%i in ('dir /b *') do (
  if not "%%~nxi"=="%~nx0" (
    ren "%%i" "!a!" 
    set /a a+=1
 ) 
) 

If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.

@echo off & setlocal EnableDelayedExpansion 

set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
  ren "%%i" "!a!.jpg" 
  set /a a+=1
) 

[Update] Here're explanations for the lines mentioned in Jack's comment.

  • setlocal EnableDelayedExpansion

In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.

For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.

  • for /f "delims=" %%i in ('dir /b *.jpg')

Here dir with /b option, lists only file names of all jpg files.

The for loop traverses and renames all jpg files.

For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.

For for loop, please refer to the page http://ss64.com/nt/for_f.html.

  • if not "%%~ni"=="%~n0"

I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.

It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.

%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.

like image 188
Landys Avatar answered Oct 10 '22 05:10

Landys


There is no need for a batch script. A simple one liner from the command line can do the job :-)

I use DIR /B to generate the list of files, piped to FINDSTR to number the files, all enclosed withn FOR /F to parse the result and perform the rename.

for /f "delims=: tokens=1*" %A in ('dir /b *.jpg^|findstr /n "^"') do @ren "%B" "%A%~xB"

Double the percents if you want to put the command in a batch script.

like image 32
dbenham Avatar answered Oct 10 '22 06:10

dbenham