日期:2014-05-17  浏览次数:21707 次

如何用CMD读写INI文件
我有一个INI文件:config.ini
[Common]
ITEM1=ITEM_1
ITEM2=ITEM_2
ITEM3=ITEM_3

1.我想读[Common]ITEM2的值,
2.重写[Common]ITEM2的值为"test2"
要怎么写这个bat文件呀

------解决方案--------------------
掉了一句,再发一次:
BatchFile code
@echo off & setlocal enabledelayedexpansion
set "f="
set "i="
for /f "delims=" %%a in (config.ini) do (
  set v=%%a
  if "!v:~0,1!"=="[" ( 
    if %%a==[Common] (set f=1) else (set "f=")
  ) else (
    if defined f (
      for %%b in (%%a) do (
        if %%b==ITEM2 (set f=2) else (if !f!==2 set i=%%b&&goto readover)   
      )
    )
  )
)

:readover
if defined i (echo ITEM2=%i%)

set "f="
(for /f "delims=" %%a in (config.ini) do (
  set v=%%a
  if "!v:~0,1!"=="[" ( 
    if %%a==[Common] (set f=1) else (set "f=")
    echo.%%a
  ) else (
    if defined f (
      if defined i (
        if "%%a"=="ITEM2=%i%" (echo ITEM2=test2) else echo.%%a 
      ) else (
        set "f="
        echo.%%a
        echo ITEM2=test2
      )
    ) else echo.%%a
  )
))>config.ini2

move config.ini2 config.ini
pause