0

Hi I do need to extract the last part of a string after the last dot Example:

1.2.37 ==> I need the 37
1.2.567 ==> I need the 567

as you can see the number of characters after the dot is not fixed so expressions like

base=%fullver:~0,-2%

Can't be used. How can I achieve this?

carlosgmercado
  • 284
  • 6
  • 17

5 Answers5

2
@echo off
    setlocal enableextensions disabledelayedexpansion

    set "fullver=1.2.456"
    for %%a in ("%fullver:.=\%") do set "base=%%~na"

    echo %base%

The trick is to replace the dots with backslashes, process the string as a path and retrieve the name of the last element in it.

Alternatively, if all the elements need to be retrieved, instead of a for, a for /f is used to tokenize the variable using the dots as separators

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "fullver=1.2.456"
    for /f "tokens=1-3 delims=." %%a in ("%fullver%") do (
        set "major=%%a"
        set "minor=%%b"
        set "build=%%c"
    )

    echo [%major%] [%minor%] [%build%]
MC ND
  • 69,615
  • 8
  • 84
  • 126
1

I found the following question which actually tokenizes the string.

How to split a string in a Windows batch file?

May be you can try using this to delimit it with "." and take the last value stored in the string variable. Not sure if there is a simple way, but this works.

Here is an edited Version to fit your Needs:

@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=1.2.5.234

for /f "tokens=1 delims=." %%a IN ("!teststring!") DO set firststring=%%a
echo !firststring!

REM Do something with each substring
:stringLOOP
    REM Stop when the string is empty
    if "!teststring!" EQU "" goto END
    for /f "delims=." %%a in ("!teststring!") do set substring=%%a
REM Now strip off the leading substring
:striploop
    set stripchar=!teststring:~0,1!
    set teststring=!teststring:~1!
    if "!teststring!" EQU "" goto stringloop
    if "!stripchar!" NEQ "." goto striploop
    goto stringloop
:END
echo !substring!
endlocal
Community
  • 1
  • 1
user3249346
  • 118
  • 6
0

I prefer MC ND's answer if you are looking for only the last node, or if you know how many nodes there are.

Here is a method to capture all nodes if the total number of nodes is unknown:

@echo off
setlocal enableDelayedExpansion

set "fullver=1.2.456"

:: Parse each node and store in an "array"
set cnt=0
for %%A in (%fullver:.= %) do (
  set /a cnt+=1
  set "node.!cnt!=%%A"
)

:: Show the results
for /l %%N in (1 1 %cnt%) do echo node.%%N = !node.%%N!
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

Another solution! This one gets the first and last parts of the string:

@echo off
setlocal

set "testString=1.2.5.234"

set "first="
for %%a in ("%testString:.=" "%") do (
   if not defined first set "first=%%~a"
   set "last=%%~a"
)

echo First: %first%
echo Last:  %last%

As a bonus, this method correctly process special Batch characters that may appear in the string, excepting wild-cards.

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

You can use the below command to achieve what you want.

base=%fullver:~~4,3%

4 implies 4th digit i.e., 5 and 3 implies 3 digits from 4.

The output will be

567
Sumith08
  • 582
  • 6
  • 16