get the last 2 tokens of a variable in Batch

2

I need to get the last 2 tokens of a variable in batch

the variable is

Rastreando a rota para user722-PC [192.168.1.106]

The output I need is a variable containing

user722-PC

and another containing

[192.168.1.106]

and no, I can not use

for /f "tokens=5,6 delims= " %%a in ("%variable%") do set host=%%a & set ip=%%b
echo %ip% %host%

In this case I am specifying token 5.6 and what I want to do is to get the last 2 tokens dynamically, so I can not specify any token numbers manually

FOR %%a in (%variable%) do set lastPart=%%a
ECHO %lastPart%
    
asked by anonymous 25.10.2017 / 08:30

1 answer

1

Would that be?

@echo off
set _var_=Rastreando a rota para user722-PC [192.168.1.106]
for /f "tokens=5,6* delims=^ " %%i in ('echo/%_var_%') do @echo/%%i %%j

Exit:

user722-PC [192.168.1.106]
    
18.11.2018 / 19:25