如何在批处理文件中替换部分IP地址(How to replace part of IP Address in batch file)

编程入门 行业动态 更新时间:2024-10-19 11:41:06
如何在批处理文件中替换部分IP地址(How to replace part of IP Address in batch file)

我想在批处理文件中替换IP地址的某些部分

让我们说有一个变量

set ip=10.20.45.254

要么

set ip=10.20.45.2

我必须将最后一部分更改为1

10.20.45.254 -> 10.20.45.1

我试过检查这本手册http://ss64.com/nt/syntax-replace.html但我不确定如何准确检测IP的最后部分,因为它可以有不同的长度...

有可能吗?

I would like to replace certain part of IP Address in a batch file.

Lets say there is a variable

set ip=10.20.45.254

or

set ip=10.20.45.2

and i have to change the last part to 1 like

10.20.45.254 -> 10.20.45.1

I have tried to check this manual http://ss64.com/nt/syntax-replace.html but I am not sure how to detect exactly the last part of IP because it can have different length...

Is it possible todo?

最满意答案

批量试用(用你的文件名替换test.txt)

for /F "tokens=1,2,3,4 delims=." %%a in (test.txt) do (echo %%a.%%b.%%c.1)

PS试试这个......

rem ----------------------------------------- rem Imagine that %line has some IP string... SET _IP=%line:~4% ECHO IP: %_IP% rem ----------------------------------------- timeout /t 2 for /f "tokens=1,2,3,4 delims=." %%a IN ("%_IP%") DO ( set gate=%%a.%%b.%%c.1 ) ECHO Gateway IP: %gate%

照顾好qoutes,它们是必不可少的("%_IP%") 。

Try this in batch (replace test.txt with your file name)

for /F "tokens=1,2,3,4 delims=." %%a in (test.txt) do (echo %%a.%%b.%%c.1)

P.S. Try this...

rem ----------------------------------------- rem Imagine that %line has some IP string... SET _IP=%line:~4% ECHO IP: %_IP% rem ----------------------------------------- timeout /t 2 for /f "tokens=1,2,3,4 delims=." %%a IN ("%_IP%") DO ( set gate=%%a.%%b.%%c.1 ) ECHO Gateway IP: %gate%

Take care of the qoutes, they are essential ("%_IP%").

更多推荐

本文发布于:2023-08-01 02:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353959.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理文件   地址   如何在   IP   replace

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!