0

I would appreciate method which can rename files with random names into the following: 00, 01, 02, ...

Is there any automated method?

user3162968
  • 1,016
  • 1
  • 9
  • 16
  • Can you be more specific. Any file on your system? Or scoped to a particular directory. From cmd shell? Code? What language? – selbie Feb 24 '14 at 09:25
  • Particular directory, for example one folder with 80 jpg files. I do not know shell or etc. yet, any other method? Or with simple explanation where shell scripts should be used:) – user3162968 Feb 24 '14 at 09:27

2 Answers2

2

Borrowing from these answers here and here and on my own to figure out how to prefix a "0" in front of files 0-9, the following is what I came up with, a simple renumber.bat file can be created.

setlocal enabledelayedexpansion
set /a count=0
for /r %%i in (*.jpg) do (
    echo %%i
    if !count! LSS 10 rename %%i 0!count!.jpg
    if !count! GEQ 10 rename %%i !count!.jpg
    set /a count+=1
)

Save the above code snippet to a file called "renumber.bat" in the directory of jpg files you want to rename. Then simply invoke "renumber.bat" from the command line. Of course, your should backup all these files anyway. It's always possible my script has a bug...

Community
  • 1
  • 1
selbie
  • 100,020
  • 15
  • 103
  • 173
1

This should do:

@SETLOCAL ENABLEDELAYEDEXPANSION

@set /a counter=1
@set counterFormatted="1"
@for /f "tokens=*" %%f in ('dir /b *.jpg') do @(
  @set counterFormatted=00000!counter!
  @rename %%f !counterFormatted:~-5!.jpg
  @set /a counter = !counter! + 1
)

It will automatically pad the number too. You can modify the padding easily by changing the amount of zeroes added to counterFormatted and modifying the ~-5 in the rename based on your requirements.

Just put it in a bat file somewhere and run it in the directory with your files. If you want to rename files with different extensions, you can modify the filter in the dir just as you're used to, and then the rename line like so:

@rename %%f !counterFormatted:~-5!%%~xf

Be careful not to put the bat file in the same folder in that case, otherwise it will be renamed as well :)

Luaan
  • 62,244
  • 7
  • 97
  • 116