Board logo

标题: [文本处理] [已解决]BAT怎么修改ini文件中某行中指定字符? [打印本页]

作者: 无敌大怪兽    时间: 2020-2-10 11:23     标题: [已解决]BAT怎么修改ini文件中某行中指定字符?

举例 配置.ini中的内容如下 (省略号表示其他行的内容)
[Launcher]

CommandLine = -connect=192.168.200.1  -port=80 -name=wuwu
...
...
...
...
Lanport = 80

就是把CommandLine行中-port=后面的端口号替换成指定的内容行中其他的不变,比如改成5555。

[Launcher]

CommandLine = -connect=192.168.200.1  -port=5555 -name=wuwu
...
...
...
...
Lanport = 80

这里的端口号是不固定可能是任意数值,不管-port=后面是啥都直接替换。
作者: zaqmlp    时间: 2020-2-10 20:37

  1. @echo off
  2. set info=互助互利,支付宝扫码头像,感谢打赏
  3. rem 有问题,可加QQ956535081及时沟通
  4. title %info%
  5. cd /d "%~dp0"
  6. set "inifile=配置.ini"
  7. set newport=5555
  8. powershell -NoProfile -ExecutionPolicy bypass ^
  9.     $enc=[Text.Encoding]::Default;^
  10.     $text=[IO.File]::ReadAllText('%inifile%', $enc);^
  11.     $text=$text -replace '(?^<=CommandLine.+?-port=)\d+','%newport%';^
  12.     [IO.File]::WriteAllText('%inifile%', $text, $enc);
  13. :end
  14. echo;%info%
  15. pause
  16. exit
复制代码

作者: 无敌大怪兽    时间: 2020-2-10 22:03

回复 2# zaqmlp


    测试通过,膜拜大佬。太感谢了
作者: zaqmlp    时间: 2020-2-10 22:12

回复 3# 无敌大怪兽

那就赞助下吧




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2