Pick up only part of the IP

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AdilsonD
Posts: 2
Joined: 08 Aug 2014 11:39

Pick up only part of the IP

#1 Post by AdilsonD » 08 Aug 2014 11:55

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.
Last edited by AdilsonD on 08 Aug 2014 12:17, edited 1 time in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Pegar apenas parte do IP

#2 Post by Squashman » 08 Aug 2014 12:14

We mostly use the English Language to communicate on the forums. If you translate your question into English you may get a quicker response.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Pegar apenas parte do IP

#3 Post by Squashman » 08 Aug 2014 12:24

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

AdilsonD
Posts: 2
Joined: 08 Aug 2014 11:39

Re: Pegar apenas parte do IP

#4 Post by AdilsonD » 08 Aug 2014 12:30

It worked perfectly. Thank you.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Pick up only part of the IP

#5 Post by Yury » 09 Aug 2014 00:01

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

Post Reply