I would like to rename and rotate the folders name with a DOS batch script.
Example:
I have this:
C:\Main\Folder01
C:\Main\Folder02
C:\Main\Folder03
C:\Main\Folder04
C:\Main\Folder05
I need to rename the Folder01 to be the last in the list, in the example it wil become Folder06. but it could be Folder50 depending of the quantity of folders. so i want to get this.
C:\Main\Folder02
C:\Main\Folder03
C:\Main\Folder04
C:\Main\Folder05
C:\Main\Folder06
Then i need to re index all the folders and to 01 02 03 04 05, so Folder02 will become Folder01 and in this way rotate all the folders.
I don't have a clue how to batch this, please advice.
I would approach the problem a bit differently. It would be easier to first decrease all the numbers (including 01
) by one:
┌─────> C:\Main\Folder00
C:\Main\Folder01 ─┘┌────> C:\Main\Folder01
C:\Main\Folder02 ──┘┌───> C:\Main\Folder02
C:\Main\Folder03 ───┘┌──> C:\Main\Folder03
C:\Main\Folder04 ────┘┌─> C:\Main\Folder04
C:\Main\Folder05 ─────┘
then rename the 00
folder to the former last name:
C:\Main\Folder00 ──┐
C:\Main\Folder01 │ C:\Main\Folder01
C:\Main\Folder02 │ C:\Main\Folder02
C:\Main\Folder03 │ C:\Main\Folder03
C:\Main\Folder04 │ C:\Main\Folder04
└─> C:\Main\Folder05
It seems easier this way because the loop renaming the folders would also be storing the last processed name in a variable, and that variable would afterwards be used to rename the 00
folder. Here's an implementation of what I am talking about:
@ECHO OFF
SET "pathtemplate=C:\Main\Folder"
FOR /D %%I IN ("%pathtemplate%??") DO (
SET "oldname=%%~nxI"
SETLOCAL EnableDelayedExpansion
SET /A "newsuffix=1!oldname:~-2!-1"
RENAME "%%I" "!oldname:~0,-2!!newsuffix:~1!"
ENDLOCAL
)
RENAME "%pathtemplate%00" "%oldname%"
The oldname
variable is the one that is used to remember the last processed folder after the loop. It is also used in the loop: first, for extracting the number, and second, for providing the base for the new name.