Batch file 使用批处理编辑外部批处理配置值
我有一个名为config.conf的配置文件,内容如下:Batch file 使用批处理编辑外部批处理配置值,batch-file,external,config,editing,Batch File,External,Config,Editing,我有一个名为config.conf的配置文件,内容如下: [Config] password=1234 usertries=0 allowterminate=0 我只想将usertrytes值编辑为5,是否有任何批处理脚本代码可以帮助我做到这一点 谢谢这有点混乱,但应该可以: @echo off set ConfigFile=config.conf setlocal enabledelayedexpansion (for /f "delims=" %%L in (%ConfigFile%
[Config]
password=1234
usertries=0
allowterminate=0
我只想将usertrytes值编辑为5,是否有任何批处理脚本代码可以帮助我做到这一点
谢谢这有点混乱,但应该可以:
@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
基本上,我们将每一行写入一个新文件,除非它以usertrys=
开头。在这种情况下,我们只需插入替换行。之后,我们将新文件移到旧文件之上以替换它
@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
我知道我参加聚会有点晚了,但我决定编写一个通用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
您准备使用哪些脚本语言?我的配置在一个名为test的文件夹中,因此我必须正确设置set conffile=test/config.conf?我还可以知道此行设置了什么“line=%%L”,因为我可能会更改用户的行号。这是我的代码集conffile=test/config.conf setlocal enabledelayedexpansion(对于(%ConfigFile%)中的/f%%L,请执行(如果“!LINE:~0,10!”==”usertrys=“(echo usertrys=0)else(echo!LINE!),请设置“LINE=%%L”)>%ConfigFile%.new move/y%ConfigFile%.new%ConfigFile%>nul
test\config.conf
应该是正确的。通常Windows不关心/
或`,但命令处理器在某些情况下会做一些奇怪的事情。
设置“LINE=%L”`只需将当前行存储在环境变量中,以便对其进行进一步处理(您无法从中为变量获取子字符串)。我不确定您出了什么问题,它在我的测试中起了作用。但是,您似乎将usertrys=0
写入文件,而不是5
。