I need to extract the Major , Minor and Revision numbers from a string and to achieve this I\'m trying to split a string in a batch file using a \'.\' character as the delim
Not have enough reputation to comment. Posting an answer instead :)
There's a helpful resource: http://ss64.com/nt/syntax-substring.html
SET _test=123456789abcdef0
::Skip 7 characters and then extract the next 5
SET _result=%_test:~7,5%
ECHO %_result% =89abc
Use this approach to extract substrings from the string as you want.
UPDATE:
Get position of a character in a string
As you get the dot position, you can use the above described approach to extract necessary parts from the input string.
Here you go...
@ECHO OFF & SETLOCAL
set /p "ReleaseVersion=Please specify the software release version : "
for /F "tokens=1,2,3 delims=." %%a in ("%ReleaseVersion%") do (
set Major=%%a
set Minor=%%b
set Revision=%%c
)
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%
Input/output:
Please specify the software release version : 1.0.2
Major: 1, Minor: 0, Revision: 2
I recently discovered an interesting trick that allows to split a string based on a delimiter without using for
command, so I couldn't resist the temptation to post it here. This question also represent a different way to use my method (split string in more than 2 different variables). Here it is:
@echo off
setlocal EnableDelayedExpansion
set /p "ReleaseVersion=Please specify the software release version : "
set "v=Minor"
set "Major=%ReleaseVersion:.=" & (if defined Minor set v=Revision) & set "!v!=%"
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%
Further details at this post.
EDIT: Some explanations added
You may read a full description of the split method here. However, this answer also use an if defined Minor
command in order to change the variable name from "Minor" to "Revision" after Minor variable value was set; this point can be easily understood if you remove the @echo off
command and review the trace output.
Although this method allows to define more than two variables with different names, it is somewhat cumbersome to insert a & (if defined prevVar set v=newVar)
command for each additional variable. For example:
@echo off
setlocal EnableDelayedExpansion
set "str=1.0.2.25"
set "v=Minor"
set "Major=%str:.=" & (if defined Minor set v=Revision) & (if defined Revision set v=Subrev) & set "!v!=%"
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%, Subrev: %Subrev%
The splitting of a string in several different variables can the achieved in a better way with the aid of a subroutine:
@echo off
setlocal EnableDelayedExpansion
call :SplitString "1.0.2.35" Major Minor Revision Subrev
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%, Subrev: %Subrev%
goto :EOF
:SplitString string varName ...
rem Get string in "str" variable
set "str=%~1"
rem Create "v" array with variable names
set i=0
for %%a in (%*) do (
set "v[!i!]=%%a"
set /A i+=1
)
rem Separate the string in individual variables
set "p=%%"
set i=1
set "!v[1]!=%str:.=" & set /A i+=1 & call set "!p!v[!i!]!p!=%"
exit /B
The split method used in this subroutine is explained with detail in this post, but I copied here the relevant parts and modified they for this example:
Note that the call set "!p!v[!i!]!p!=%"
part is a nested replacement that is executed with each one of the substrings of the original replacement. This way, this method is comprised of three stages:
call set "!p!v[!i!]!p!=1"
, call set "!p!v[!i!]!p!=0"
, etc.call set "%v[1]%=1"
, call set "%v[2]%=0"
, etc.set "Major=1"
, set "Minor=0"
, etc.EDIT 2018/02/18: New method added
I developed a new method that allows to assign values to different variables using just one line. This method is new in the sense that allows to split two variables in the same line/replacement:
@echo off
setlocal EnableDelayedExpansion
set "str=1.0.2.25"
set "vars=Major Minor Revision Subrev"
set "p=%%"
set "v=%vars: =" & set "s=!str:*.=!" & call set "!v!=!p!str:.!s!=!p!" & set "str=!s!" & set "v=%" & set "!v!=!s!"
echo Major: %Major%, Minor: %Minor%, Revision: %Revision%, Subrev: %Subrev%