返回列表 发帖

[文本处理] 求助,批处理如何修改ini文件的指定键值对?

[Path]
;产品名
Project=I7
;分支号
Branch=R1.2
;产品名+分支号
ProBranch=I7_R1.2
;源文件路径
SourcePath=H:\i7standard\I7_R1.2\Platform\UI\bin\Release\
;需更新文件路径
NeedUpdatePath=H:\i7standard\I7_R1.2\



这是test.ini文件的全部内容,如何将SourcePath和NeedUpdatePath的值修改为c:\windows

回复 1# yym黄诗瑶
@echo off
set rip=c:\windows
set tihuan1=SourcePath=
set tihuan2=NeedUpdatePath=
echo %tihuan1%
echo %tihuan2%
pause
del /f /q new.ini
findstr /n .* 1.ini>temp.ini
for /f "tokens=1* delims=:" %%a in ('findstr /b /n %tihuan1% 1.ini') do (
set /a tn=%%a
for /f "tokens=2 delims==" %%c in ("%%b") do set tip=%%c
)
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in (temp.ini) do (
set /a ln=%%i
set line=%%j
if !ln! equ !tn! (
echo;!line:%tip%=%rip%!
) else (
echo;!line!
)
)>>new.ini
1pause
del /q 1.ini
ren new.ini 1.ini
1pause
del /f /q temp.ini
findstr /n .* 1.ini>temp.ini
for /f "tokens=1* delims=:" %%a in ('findstr /b /n %tihuan2% 1.ini') do (
set /a tn=%%a
for /f "tokens=2 delims==" %%c in ("%%b") do set tip=%%c
)
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in (temp.ini) do (
set /a ln=%%i
set line=%%j
if !ln! equ !tn! (
echo;!line:%tip%=%rip%!
) else (
echo;!line!
)
)>>new.ini
1pause
del /q 1.ini
ren new.ini 1.ini
1pause
del /f /q temp.ini
1pause
exitCOPY

TOP

回复 2# ygqiang


这样比较简洁
@echo off
setlocal enabledelayedexpansion
(for /f "tokens=1* delims=:" %%h in ('findstr /n "." "1.ini"') do (
    set "str=%%i"
    if "!str:SourcePath=!" neq "%%i" (
        echo SourcePath=C:\Windows
    ) else if "!str:NeedUpdatePath=!" neq "%%i" (
        echo NeedUpdatePath=C:\Windows
    ) else (
        echo,%%i
    )
))>"2.ini"COPY

TOP

本帖最后由 codegay 于 2017-3-22 01:49 编辑

回复 3# ShowCode


    INI文件的格式是:
[节点]
选项=xxxCOPY
用等号定位比较靠谱。冒号不是必然会出现的。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

本帖最后由 codegay 于 2017-3-22 08:36 编辑

回复 1# yym黄诗瑶


    你们的打包安装程序用的是什么工具?Inno ?还是NSIS?
这类工具是集成了对INI文件的读写的功能的。如果是安装过程中进行处理,完全可以交给安装程序来处理。
INNO的文档如下:
[INI]
Filename: "MyProg.ini"; Section: "InstallSettings"; Flags: uninsdeletesection
Filename: "MyProg.ini"; Section: "InstallSettings"; Key: "InstallPath"; String: "{app}"COPY
少部分系统目录不是c:\windows , 用系统变量systemroot可能是比较靠谱,可以规避掉这种可能是的意外。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 4# codegay


    冒号是 findstr 生成的,跟 ini 文件没啥关系吧。

TOP

回复 5# codegay

是在打包之前要进行批量修改,所以写在nsi里面达不到我的需求

TOP

回复 3# ShowCode


    有用,只是分号注释部分有的会乱码

TOP

回复 8# yym黄诗瑶


    你需要说明INI的文本编码
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

应该是UTF8的吧。、
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

powershell "(gc 'test.ini') -replace '(?<=^\s*?(SourcePath|NeedUpdatePath)\s*?=).*','c:\windows'"&pauseCOPY

TOP

回复 8# yym黄诗瑶


用记事本打开原始的ini
文件
另存为
选择 ANSI 编码
再执行批处理脚本试试

TOP

回复 1# yym黄诗瑶
So easy
@echo off&setlocal enabledelayedexpansion
chcp 65001
(
for /f "delims= eol=" %%a in (src.ini) do (
2>NUL set "%%a" && (
if defined SourcePath (
echo;SourcePath=C:\Windows
set SourcePath=
) else if defined NeedUpdatePath (
echo;NeedUpdatePath=C:\Windows
set NeedUpdatePath=
) else (
echo;%%a
)
) || (
echo;%%a
)
)
)>.\new.ini
chcp 936
start .\new.iniCOPY

TOP

研究了一下没怎么看看懂

如果有哪里不懂的话,可以回帖提问。

TOP

回复 3# ShowCode


    没有明白你的思路,for /f "tokens=1* delims=:" %%h in ('findstr /n "." "1.ini"') do echo %%h  就是打印这个文本有多少行,
   for /f "tokens=1* delims=:" %%h in ('findstr /n "." "1.ini"') do  echo %%i 就是把我文本整个打印出来了
,后面你 set "str=%%i"没懂,"!str:SourcePath=!"这个也没懂

TOP

返回列表