0

I've looked around, and there are a lot of posts very similar to this but I can't seem to find an answer that will work for what I need.

I have a big list of files with different extensions that I want to rename to numbers. For example

SomeFileName1.jpg
SomeFileName2.gif
SomeFileName3.mkv

changed to:

1.jpg
2.gif
3.mkv

I want to keep the extensions the same. Only the name should change to 1,2,3 etc.

A while ago I found some code that renamed all the files (don't have the code anymore), but it renamed them in the wrong order. "SomeFileName3.mkv" became "1.mkv" instead of "3.mkv" for example. I'm not sure if I need to sort them first somehow? I'm not very good at this kind of thing so I could really use some help. Thanks

dbmitch
  • 5,361
  • 4
  • 24
  • 38

1 Answers1

0

use a for loop to process all files. Use dir to sort the files to your needs. Use a counter. Use delayed expansion to make the counter work.

@echo off
setlocal enabledelayedexpansion
set count=0
for /f "delims=" %%a in ('dir /b /on /a-d') do (
  set /a count +=1
  ECHO ren "%%a" "!count!%%~xa"
)

for more information read for /?, dir /? and set /?

If the output satisfies you, remove the ECHO to arm the ren command.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91