get the last 2 tokens of a variable in Batch

Question:

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't use

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

because in this case I'm specifying token 5,6 and what I want to do is get the last 2 tokens dynamically, so I can't specify any token number manually

// this way you can only get the last token, considering that the delimiter is a space, and there is no way to change the delimiter

FOR %%a in (%variable%) do set lastPart=%%a
ECHO %lastPart%

Answer:

Would it be this?

@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]
Scroll to Top