First question here. I’ve used batch files for automating a lot of action but this one is getting really difficult.
Imagine I have a folder (lets call it ‘ProjectFolder’) and I have some folders inside this project folder (lets call them ‘data’, ‘tools, ‘trash’) and each of these folders have files.
What I want is to create inside the ‘ProjectFolder’ a ‘backup’ folder and inside this one a folder called ‘v001_date_time_user’ and copy folders ‘data’ and ‘tools’ inside this folder.
Then I run this script again to make a new version and it creates ‘v002_date_time_user’ and copies the same ‘data’ and ‘tools’ folders and files again.
What I can do now is create a folder called ‘v_date_time_user’ inside the ‘backup’ folder but I have no idea how to create the version part with 3 digits.
This is my code for now:
@echo off & setlocal enableextensions
:: variables
set dateNtime="[%date:~6,6%-%date:~3,2%-%date:~0,2%]_[%time:~0,2%-%time:~3,2%]"
set backup="backup"
set /A version=000
set backupcmd=xcopy /s /c /d /e /h /i /r /k /y
:: check for existence of [backup]
:: if [backup] doesn't exist, create it
if not exist "%backup%\" (
echo folder "%backup%" not found
echo creating folder "%backup%"
md "%backup%"
)
:: create version folder with version number_date_hour_user
md "%backup%\v%version%_%dateNtime%_[%USERNAME%]"
:: copy older version into newer version
xcopy "data" "%backup%\v%version%_%dateNtime%_[%USERNAME%]\data" /E /C /I /H /Q
xcopy "tools" "%backup%\v%version%_%dateNtime%_[%USERNAME%]\tools" /E /C /I /H /Q
can anyone help?
thanks
Luís
There are a couple of ways I can think of. You could loop through every folder beginning with a v and a number, and count them. Then if you’re starting with 000 then the number of matches will be your next number.
Then
%count%will be your next number.Another way would be to loop through
dir /b /o:n(alphabetic sorting) and capture the last directory name, presumably v### with the highest number. Then grab the first four characters of that capture and chop off the v.