6

In a batch file I have a variable containing an IP. Just for example:

SET ip = 170.150.120.10

I would like to define another variable, replacing the last octet with 1, which in the example would be 170.150.120.1.

The IP may change (it is defined dynamically), varying in its length, so capturing a fixed length substring and concatenating it would not work anytime (in the example it would be SET baseIP = %ip:~0,9%.1).

How can this task be solved?
Is there some RegEx support in Windows command line?

2
  • 1
    your ip last part is it only max .10 or it can be more then .100 OR would you first part of ip change to like 2.150.120.10 Commented Aug 18, 2015 at 16:42
  • 1
    The IPv4 to manipulate could vary from 0.0.0.0 to 255.255.255.255, covering both all private and public IPs. Commented Aug 19, 2015 at 15:31

3 Answers 3

6

maytham-ɯɐɥıλɐɯ has the key component of a simple solution - FOR /F. But that solution has a lot of complication that seems unrelated to the question.

The answer can be as simple as:

@echo off
set "ip=170.150.120.10"
for /f "tokens=1-3 delims=." %%A in ("%ip%") do set "new_ip=%%A.%%B.%%C.1"
echo new ip=%new_ip%


Note - You included spaces before and after the = in the SET statement in your question. That is a bad idea, as all of the spaces are significant. You have a variable name that ends with a space, and a value that begins with a space. I removed the unwanted spaces from the answer

Also, I enclosed the assignment within quotes. All characters after the last quote are ignored as long as the first quote is before the variable name. This protects against inadvertent trailing spaces in your value.

EDIT 2017-09-04
Even simpler method - treat the address as a filename, so the last node becomes the extension. Use a simple FOR and the ~n modifier to get the base name (1st 3 nodes), and then add your own extension (last node).

for %%A in (%ip%) do set "new_ip=%%~nA.1"
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks very much for your answer, I didn't know it was possible providing multiple options for the FOR statement (tokens and delims). This way the parsing is correct, no matter how long is any octet. Thanks also for the advice of delimiting variable assignment with quotes.
2

(Edit: added missing jump)

Here's my take. Iterates over the last four characters, looks if it is a dot, and appends the desired octet to the corresponding prefix part of the given IP. This works with any size (length) of last octet, e.g. 1.1.1.5 and 10.0.0.155

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET ip=170.150.120.10
SET new_ip_last_octet=1
ECHO Input was %ip%

FOR /L %%G IN (0,-1,-4) DO (
    SET tmp=!ip:~%%G!
    IF "!tmp:~0,1!" == "." (
        SET new_ip=!ip:~0,%%G!.!new_ip_last_octet!
        GOTO done
    )
)
:done
ECHO New IP is %new_ip%

Output:

Input was 170.150.120.10
New IP is 170.150.120.1

Comments

2

Try this

@echo off

set ipCurrent=170.150.120.100
set ipOffsets=0.100.0.-24

@echo off
  for /f "tokens=1-3 delims=. " %%a in ("%ipCurrent%") do (
    set part1=%%a
    set part2=%%b
    set part3=%%c
)
  for /f "tokens=1-3 delims=." %%a in ("%ipOffsets%") do (
    set /a part1+=%%a
    set /a part2+=%%b
    set /a part3+=%%c
)

set ipBase= %part1%.%part2%.%part3%.1
@echo %ipCurrent% is changed to%ipBase%

EDIT Thanks to @dbenham for input, the code above can be reduced to:

@echo off

set "ipCurrent=170.150.120.100"
@echo off
  for /f "tokens=1-3 delims=. " %%a in ("%ipCurrent%") do set "ipBase=%%a.%%b.%%c.1"

@echo %ipCurrent% is changed to %ipBase%

Input any ip address range

input  170.150.120.10
or     170.150.120.110

Output 170.150.120.1

Batch resources: Link

2 Comments

Thanks very much @maytham-ɯɐɥıλɐɯ, the resource you linked is very useful, treating the string manipulation almost similarly to some modern programming language guide.
The code I provided was used for another project, I just copied to you. but it is true it can be reduced as @dbenham wrote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.