Page 1 of 1

Pick up only part of the IP

Posted: 08 Aug 2014 11:55
by AdilsonD
Excuse me ...

Hello everyone,

I use the function "for" below to get the IP of the machine, but I need to change the last set of digits. I know the second column does this, but sometimes the ip can have 1,2 or 3 digits and how this function, it does not work.

ex:

192.168.1.1 -> must return 192.168.1.
192.168.1.12 -> must return 192.168.1.
192.168.1.123 -> must return 192.168.1.

192.168.1.1 -> preciso que retorne 192.168.1.
192.168.1.12 -> preciso que retorne 192.168.1.
192.168.1.123 -> preciso que retorne 192.168.1.

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "IPv4."') do for %%b in (%%a) do set ip=%%b
if /i %ip% NEQ 1 set ip=%ip:~0,-2%

Thank you.

Re: Pegar apenas parte do IP

Posted: 08 Aug 2014 12:14
by Squashman
We mostly use the English Language to communicate on the forums. If you translate your question into English you may get a quicker response.

Re: Pegar apenas parte do IP

Posted: 08 Aug 2014 12:24
by Squashman
I think this is what you want to do.

Code: Select all

@echo off
for /f "tokens=2 delims=:" %%G in ('ipconfig ^| find "IPv4"') do (
   for /F "tokens=1-4 delims=. " %%H in ("%%G") do (
      set octet1=%%H
      set octet2=%%I
      set octet3=%%J
      set octet4=%%K
   )
)
echo %octet1%.%octet2%.%octet3%.%octet4%
pause

Re: Pegar apenas parte do IP

Posted: 08 Aug 2014 12:30
by AdilsonD
It worked perfectly. Thank you.

Re: Pick up only part of the IP

Posted: 09 Aug 2014 00:01
by Yury

Code: Select all

@echo off
for /f "tokens=2 delims=:" %%a in ('ipconfig^| find "IPv4"') do set ip=%%a
set ip=%ip: =%
:#
echo %ip%| findstr \.[0-9][0-9][0-9]*$>nul&& (set ip=%ip:~,-1%& goto:#)
set ip
pause>nul
exit /b