I want to number files based on creation date rather than based on the old file names.
Example: Assume the following files are listed in descending order of creation date.
A.jpg
D.jpg
B.jpg
C.jpg
I want to rename these files such that
A.jpg becomes 4.jpg
D.jpg becomes 3.jpg
B.jpg becomes 2.jpg
C.jpg becomes 1.jpg
I am using Windows 8 and have sorted the files in the folder based on creation date. However, when I try to rename files, DOS ignores the sorting and proceeds to rename files based on old file names.
Thus,
A.jpg becomes 1.jpg
D.jpg becomes 4.jpg
B.jpg becomes 2.jpg
C.jpg becomes 3.jpg
I am using the following code (.bat file) which I got from a different post in this Website.
@Echo Off
setLocal EnableDelayedExpansion
set oldext=jpg
set newext=jpg
set index=0
for %%f IN (*.%oldext%) do (
set /A index+=1
set index=!index!
ren "%%f" "!index!.%newext%"
)
I do not have any scripting tools installed and am only creating basic .bat files using notepad.
Thanks for your help.
In FAT file systems the files are not ordered, they are placed as they are included in folders. In NTFS files are alphabetically ordered. You can change how you want to see the files, but not how they are phisically ordered.
for
loops will iterate over the files in the same order they are stored. If you need to iterate in a different order, you will need to create a list in the order you want and iterate over this list.
@echo off
setlocal enableextensions disabledelayedexpansion
set "oldext=jpg"
set "newext=jpg"
set "index=0"
for /f "delims=" %%a in ('dir /a-d /b /od "*.%oldext%"') do (
set /a "index+=1"
setlocal enabledelayedexpansion
for %%b in (!index!) do endlocal & ren "%%~fa" "%%b.%newext%"
)
Here a dir
command is used to list the files in the desired order and then this list is iterated by the for /f
command.
The inner setlocal enabledelayedexpansion / endlocal
is included to handle the case of files with !
characters in its names. If you know this will never be your case, you can directly use
@echo off
setlocal enableextensions enabledelayedexpansion
set "oldext=jpg"
set "newext=jpg"
set "index=0"
for /f "delims=" %%a in ('dir /a-d /b /od "*.%oldext%"') do (
set /a "index+=1"
ren "%%~fa" "!index!.%newext%"
)
Note that none of the included samples handle in any way the possibility of duplicate file names.