Tuesday, May 15, 2018

windows - Rename each file an incrementing integer



I have a directory of mp3 files, sorted in order of Date created:



newest.mp3
new.mp3

old.mp3
older.mp3
oldest.mp3


I want to rename each file in this directory with a new name including an integer value that increases for each file (starting at the newest file):



newest.mp3 --> new_name_1.mp3
new.mp3 --> new_name_2.mp3
old.mp3 --> new_name_3.mp3

older.mp3 --> new_name_4.mp3
oldest.mp3 --> new_name_5.mp3


How can I do this automatically (without needing to manually rename each file)?



Specifically, I'm looking for a command to essentially do this:



sort each file by date created
i = 1

for each fil in dir:
rename file: new_name_$i++.mp3

Answer



Here's a way to do that



setlocal enabledelayedexpansion
set "count=1"
for /f "delims=*" %%f in ('dir /b /o:-d /tc *.mp3') do (
ren %%f new_name_!count!.mp3

set /a count+=1
)



  • dir /b: output the files in bare format (only file names)

  • /o:-d: sort by date, newest first

  • /tc: sort using creation date. You may want to change C to A or W for access time and modification time




Then each line of the output is read using for /f



To make it work for any directory, change the loop to



for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc "%1\*.mp3"`)


and pass the directory in the first parameter



To get help for any commands in Windows cmd, just use /?





No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...