如何在批处理文件中替换部分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.254or
set ip=10.20.45.2and i have to change the last part to 1 like
10.20.45.254 -> 10.20.45.1I 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%").
更多推荐
发布评论