Rename each file an incrementing integer
7,837
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 /?
Related videos on Youtube
Author by
user9506231
Updated on September 18, 2022Comments
-
user9506231 over 1 year
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
-
shawn about 5 yearsHave you tried a batch looping with
for
ondir /od
using an incrementing counter viaset /a
? -
user9506231 about 5 yearsNo. I haven't done scripting / programming on windows before
-
-
user9506231 about 5 yearsI've updated the question to illustrate what I'm looking for
-
user9506231 about 5 yearsI'm having trouble running this as a batch file. I've saved the code to a
.bat
file in the directory above, and pasted the fullC:\Users\...
path. After running> script.bat
it saysC:\Users\...
is not recognized as an internal or external command, operable program of batch file -
phuclv about 5 years
> script.bat
is not the way to run the script because>
is a redirection operator. And what do you mean by pasted the full path? Where did you pasted? This one will run with the files in the current directory, if you want to rename files in another directory then a small change need to be done -
user9506231 about 5 yearsThe issue was I didn't have the batch file in the desired directory. It's working now. Thanks very much!
-
user9506231 about 5 yearsHowever, the batch file is no longer in the directory. Has that been renamed to a
.mp3
file as well? -
phuclv about 5 yearsyes, it's the newest/smallest file. I've added
*.mp3
to the dir command so it excludes the batch file