Get a substring from a Windows package variable

In the batch file, I have a variable containing IP. For instance:

  SET ip = 170.150.120.10 

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

IP can change (it is determined dynamically), changing in its length, so capturing a substring with a fixed length and combining it will not work at any time (in the example it will be SET baseIP = %ip:~0,9%.1 ).

How to solve this problem?
Is there RegEx support on the Windows command line?

+4
source share
3 answers

maytham-ɯɐɥıλɐɯ has the key component of a simple solution - FOR / F. But this solution has many complications that seem to be unrelated to the issue.

The answer may 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 = in the SET statement in your question. This is a bad idea, since all spaces are significant. You have a variable name that ends with a space and a value that starts with a space. I removed unnecessary spaces from the answer

In addition, I enclosed the appointment in quotation marks. All characters after the last quote are ignored if the first quote is before the variable name. This protects against inadvertent end gaps in your value.

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

 for %%A in (%ip%) do set "new_ip=%%~nA.1" 
+5
source

(Edit: added missing jump)

Here is my welcome. Iterates over the last four characters, looks if it is a period, and adds the desired octet to the corresponding prefix part of this IP. This works with any size (length) of the last octet, for example. 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 
+1
source

try it

 @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 above code 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% 

Enter any range of IP addresses

 input 170.150.120.10 or 170.150.120.110 Output 170.150.120.1 

Party Resources: Link

+1
source

All Articles