0

我有一个名为 config.conf 的配置文件,其内容为:

[Config]
password=1234 
usertries=0 
allowterminate=0 

我只想将 usertries 值编辑为 5,是否有任何批处理脚本代码可以帮助我做到这一点

谢谢

4

3 回答 3

1

这有点混乱,但应该可以:

@echo off
set ConfigFile=config.conf
setlocal enabledelayedexpansion
(for /f "delims=" %%L in (%ConfigFile%) do (
  set "LINE=%%L"
  if "!LINE:~0,10!"=="usertries=" (echo usertries=5) else (echo !LINE!)
)) > %ConfigFile%.new
move /y %ConfigFile%.new %ConfigFile% > nul

基本上我们将每一行写入一个新文件,除非它以usertries=. 在这种情况下,我们只需插入替换行。之后,我们将新文件移到旧文件之上以替换它。

于 2013-03-14T11:38:21.387 回答
0
@ECHO OFF
setlocal
::
:: set %1 to value %2 in config.conf
::
SET target=%2&IF NOT DEFINED target ECHO syntax is %0 keyname newvalue&GOTO :EOF 
SET target=Config.conf
MOVE /y %target% %target%.old >NUL
FOR /f "tokens=1*delims==" %%i IN (%target%.old) DO (
IF /i %%i==%1 (ECHO %%i=%2) ELSE (
IF "%%j"=="" (ECHO %%i) ELSE (ECHO %%i=%%j)
)
) >>%target%

使用此版本,您可以使用将现有键更改为值

thisbatchname existingkey newvalue
于 2013-03-14T13:30:42.850 回答
0

我知道我参加聚会有点晚了,但我决定编写一个通用的 ini 文件实用程序批处理脚本来解决这个问题。

该脚本将允许您检索或修改 ini 样式文件中的值。它的搜索不区分大小写,并且在 ini 文件中保留空白行。从本质上讲,它允许您将 ini 文件作为一种非常基本的数据库进行交互。

:: --------------------
:: ini.bat
:: ini.bat /? for usage
:: --------------------

@echo off
setlocal enabledelayedexpansion

goto begin

:usage
echo Usage: %~nx0 /i item [/v value] [/s section] inifile
echo;
echo Take the following ini file for example:
echo;
echo    [Config]
echo    password=1234
echo    usertries=0
echo    allowterminate=0
echo;
echo To read the "password" value:
echo    %~nx0 /s Config /i password inifile
echo;
echo To change the "usertries" value to 5:
echo    %~nx0 /s Config /i usertries /v 5 inifile
echo;
echo In the above examples, "/s Config" is optional, but will allow the selection of
echo a specific item where the ini file contains similar items in multiple sections.
goto :EOF

:begin
if "%~1"=="" goto usage
for %%I in (item value section found) do set %%I=
for %%I in (%*) do (
    if defined next (
        if !next!==/i set item=%%I
        if !next!==/v set value=%%I
        if !next!==/s set section=%%I
        set next=
    ) else (
        for %%x in (/i /v /s) do if "%%~I"=="%%x" set "next=%%~I"
        if not defined next (
            set "arg=%%~I"
            if "!arg:~0,1!"=="/" (
                1>&2 echo Error: Unrecognized option "%%~I"
                1>&2 echo;
                1>&2 call :usage
                exit /b 1
            ) else set "inifile=%%~I"
        )
    )
)
for %%I in (item inifile) do if not defined %%I goto usage
if not exist "%inifile%" (
    1>&2 echo Error: %inifile% not found.
    exit /b 1
)

if not defined section (
    if not defined value (
        for /f "usebackq tokens=2 delims==" %%I in (`findstr /i "^%item%\=" "%inifile%"`) do (
            echo(%%I
        )
    ) else (
        for /f "usebackq delims=" %%I in (`findstr /n "^" "%inifile%"`) do (
            set "line=%%I" && set "line=!line:*:=!"
            echo(!line! | findstr /i "^%item%\=" >NUL && (
                1>>"%inifile%.1" echo(%item%=%value%
                echo(%value%
            ) || 1>>"%inifile%.1" echo(!line!
        )
    )
) else (
    for /f "usebackq delims=" %%I in (`findstr /n "^" "%inifile%"`) do (
        set "line=%%I" && set "line=!line:*:=!"
        if defined found (
            if defined value (
                echo(!line! | findstr /i "^%item%\=" >NUL && (
                    1>>"%inifile%.1" echo(%item%=%value%
                    echo(%value%
                    set found=
                ) || 1>>"%inifile%.1" echo(!line!
            ) else echo(!line! | findstr /i "^%item%\=" >NUL && (
                for /f "tokens=2 delims==" %%x in ("!line!") do (
                    echo(%%x
                    exit /b 0
                )
            )
        ) else (
            if defined value (1>>"%inifile%.1" echo(!line!)
            echo(!line! | find /i "[%section%]" >NUL && set found=1
        )
    )
)

if exist "%inifile%.1" move /y "%inifile%.1" "%inifile%">NUL

例子:

C:\Users\me\Desktop>type config.conf
[Config]
password=1234
usertries=0
allowterminate=0

[Other]
password=foo

C:\Users\me\Desktop>ini /i usertries /v 5 config.conf
5

C:\Users\me\Desktop>ini /i usertries config.conf
5

C:\Users\me\Desktop>ini /s config /i password /v bar config.conf
bar

C:\Users\me\Desktop>ini /s other /i password /v baz config.conf
baz

C:\Users\me\Desktop>type config.conf
[Config]
password=bar
usertries=5
allowterminate=0

[Other]
password=baz
于 2013-03-14T14:32:59.733 回答