如何用BAT批处理更改.ini文件指定的内容

在c:\windows下有个.ini文件,要将里面指定的IP地址改成域名.如:
把其中的 "Item4=192.168.1.2" 修改成"Item4=test.test.com
谢谢各位高手!

用VBS更简单:
vbs代码:

On Error Resume Next
Dim Fso,IniFl,Str,IniFn
IniFn=" " ' 在等号后面双引号里写上ini文件的文件名,例如:IniFn="configip.ini"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set IniFl = Fso.OpenTextFile ("c:\Windows\"&IniFn,1)
Str = Replace ( IniFl.ReadAll,"Item4=192.168.1.2","Item4=test.test.com")
Set IniFl = Fso.OpenTextFile ("c:\Windows\"&IniFn,2)
IniFl.Write Str
IniFl.Close

批处理代码:

@echo off >tmp.ini
set IniFn=
::在等号后面写上ini的文件名,如:set IniFn=configip.ini
for /f "tokens=1* delims=:" %%i in ('findstr /n ".*" c:\Windows\%IniFn%') do (
if "%%j"=="" (echo.>>tmp.ini) else (
echo %%j|find "Item4=192.168.1.2">nul&&(
call set tp=%%j&call echo %%tp:192.168.1.2=test.test.com%%>>tmp.ini)||(
>>tmp.ini echo %%j)
)
)
copy tmp.ini c:\Windows\%IniFn% /y >nul||(attrib -s -a -r -h c:\Windows\%IniFn%© tmp.ini c:\Windows\%IniFn% /y >nul)
del tmp.ini
pause
温馨提示:答案为网友推荐,仅供参考
相似回答