How to rename a folder with a number (ie Folder1, Folder2, etc…)
-
14-02-2021 - |
Frage
I'm messing around with dos and batch files for the first time and I am trying to make a "program" to backup my Minecraft saves (lol). I want the program to rename the current save (in my MinecraftSaves folder) as "Backup#" before the next one is copied into the MinecraftSaves folder. Its simple enough to rename it, but I want it to save multiple folders, each incrementing a number at the end of the folder name (ie Backup1, Backup2, Backup3). Any help? I've looked all over but I couldn't find something exactly to fit my needs.
@Echo off
title Minecraft Backup
echo.
echo.
echo.
echo Do you want to backup you're Single Player World?
echo.
SET /P ANSWER=Do you want to continue (Y/N)?
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
if /i {%ANSWER%}=={n} (goto :no)
if /i {%ANSWER%}=={no} (goto :no)
:no
PING 1.1.1.1 -n 1 -w 1000 >NUL
exit
:yes
ren C:\Users\Josh\Desktop\MinecraftSaves\SinglePlayer Backup
xcopy C:\Users\Josh\AppData\Roaming\.minecraft\saves C:\Users\Josh\Desktop\MinecraftSaves /-y /e /h
Thats what I have so far. I want to change the following to rename the SinglePlayer folder as backup1, and the next time I run it have it rename the new SinglePlayer folder as backup2. I'm trying to explain this as best I can. Maybe theres a simpler way to do this. I just need it to make backups with numbers on the backup folders. Hope thats clear enough.
Lösung
Excuse me. I think I did not really understood your question. However, the following Batch file take an existent folder named CurrentSave and rename it to Backup-# adding 1 to the last Backup-# existent folder. The dash in the folder name make things easier.
@echo off
for /D %%d in (Backup-*) do set lastfolder=%%d
for /F "tokens=2 delims=-" %%n in ("%lastfolder%") do set /A nextnumber=%%n+1
move CurrentSave Backup-%nextnumber%
If this is not what you want, give us more details so we may help you.
EDIT
OK. The code I posted above works right, just a couple details are needed. Below is the code you must insert from :yes
label on:
:yes
set lastfolder=Backup-0
for /D %%d in (C:\Users\Josh\Desktop\MinecraftSaves\Backup-*) do set lastfolder=%%~Nd
for /F "tokens=2 delims=-" %%n in ("%lastfolder%") do set /A nextnumber=%%n+1
ren C:\Users\Josh\Desktop\MinecraftSaves\SinglePlayer Backup-%nextnumber%
xcopy C:\Users\Josh\AppData\Roaming\.minecraft\saves C:\Users\Josh\Desktop\MinecraftSaves /-y /e /h
May I suggest you another modification?
SET /P ANSWER=Do you want to continue (Y/N)?
for %%a in (y yes) do if /i "%ANSWER%"=="%%a" goto yes
:no
Please let me know if you solved your problem.