get the last 2 tokens of a variable in Batch


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

the variable is

Rastreando a rota para user722-PC []

the output I need is a variable containing


and another containing


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%


Would it be this?

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


user722-PC []
Scroll to Top