So I got my answer to the

How to create a folder with current date in its name and copy to it all files and folders from some path on Windows 10

question, but as it was a duplicated issue and I am a newbie on this forum- it was closed and I am unable to ask additional question in similar topics

By using this How to create a subfolder based on current date? solution I was able to achieve what I wanted [thank you very much user compo]

And so this code is working A-OK:

@Echo Off
Set "sd=C:\Users\YOUR USER NAME\AppData\Roaming\Mozilla\Firefox\Profiles\fsj89244"
Set "dd=D:\Backup Copies\Firefox\Profile"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E

It creates a sub-folder with a date [on drive D] in its name and then copies to it all content from a specified path [from the drive C]. This speeds up my manual creation of backup copies

But if the script is executed again I would need it to check first if such sub-folder exists. And if yes then leave it alone and create a folder with name in date format >>YYYY MM DD v2<<; and the next time >>YYYY MM DD v3<< etc. which would stop happening when the date in the operating system would change to a next day

1

There are 1 answers

11
Stephan On
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
set ver=0
for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1
set "newFolder=%ds% v%ver%"
md "%newFolder: v0=%"

dir /ad /on will sort the folders by name and the for ends with the last version plus one (The obvious for /d %%a in ("%ds%*") do set /a ver+=1 just counts the found folders and so will be incorrect if a folder is missing for any reason and probably will use an already "occupied" folder)

The first folder of a day will be in the format YYYY MM DD, all further folders will be in the format YYYY MM DD vX. If you want the first folder to be the same format (... v0), use just md "%newFolder%.