take the last 2 tokens of a variable in Batch

Asked

Viewed 418 times

3

I need to pick up the last 2 tokens of a variable 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 am specifying the token 5.6 and what I want to afzer is to get the last 2 tokens dynamically, so I cannot specify any token numbers manually

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

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

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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.