我有一个与应用程序相关的配置文件,该应用程序具有照常的键值对。 [常规]选项卡中的变量之一是xvariable = 0。我希望将其更改为xvariable = 1,即将新值更改为1。我不想使用外部exe或其他程序。请帮忙
很抱歉收到我的回复,请原谅,因为我是批处理脚本的新手,所以我的初始ini文件看起来像这样
[General]
DisableAutomaticallyReconnection=0
TLUSERDIR=C:\\Users\\Balu\\AppData\\Roaming\\Schlumberger\\Techlog
memoryManagment=-1
[ModuleManager]
lastUsedProfile=TechlogCoreModule_1_Default
[TNotificationService]
Info_balloon=1
Warning_balloon=1
Error_balloon=1
Command_balloon=1
InfoMacro_balloon=1
预期的输出ini在[常规]标签中仅包含一项更改,即
[General]
DisableAutomaticallyReconnection=1 (new value)
ini的其他行将保持不变。 我尝试了三种方法(对于批处理脚本来说是新方法)
脚本1
@ echo off
for /d %%F in (c:\users\*) do (
::echo %%F
set filename=%%F\AppData\Roaming\Schlumberger\Techlog 2> nul
cd %%F\AppData\Roaming\Schlumberger\Techlog 2> nul
where=$(PWD)
:: echo !where!
copy Techlog.ini techlog.orig 2> nul
type Techlog.ini|find /i /v "general"|find /i /v
"DisableAutomaticallyReconnection" > techlog1 2> nul
echo [General] > techlog2 2> nul
echo DisableAutomaticallyReconnection=1 >> techlog2 2> nul
del /q Techlog.ini 2> nul
type techlog2 techlog1 > Techlog.ini 2> nul
del /q techlog1 2> nul
del /q techlog2 2> nul
)
脚本2
@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in
(C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini) do (
set input=%%i
:: echo !input!
set input=!input:DisableAutomaticallyReconnection=1!
echo !input!
echo !input! >>
C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini.chg
)
ren C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini Techlog.ini.old
ren C:\Users\Balu\AppData\Roaming\Schlumberger\Techlog\Techlog.ini.chg Techlog.ini
脚本3
@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in (DataFile.ini) do (
set input=%%i
set input=!input:DisableAutomaticallyReconnection=test=1!
echo !input! >> DataFile.ini.chg
)
ren DataFile.ini DataFile.ini.old
ren DataFile.ini.chg DataFile.ini
谢谢。
答案 0 :(得分:1)
这是一个示例脚本,您需要对其进行编辑(在第2
行上),以定义源INI文件。
@SetLocal DisableDelayedExpansion
@Set "IFile=C:\SomePathTo\YourFile.ini"
@Set "ISect=General"
@Set "ISKey=xvariable"
@If Not Exist "%IFile%" GoTo :EOF
@Set "SName=[NULL]"
@For /F "Tokens=1*Delims=]" %%# In ('Type "%IFile%" 2^>NUL^|"%__AppDir__%find.exe" /V /N ""^&Break^>"%IFile%"')Do @(
Echo("%%$"|"%__AppDir__%findstr.exe" "^\"\[.*\]\"$">NUL&&Set "SName=%%$"
SetLocal EnableDelayedExpansion
If /I "[%ISect%]"=="!SName!" (If /I Not "[%ISect%]"=="%%$" (
Echo("%%$"|"%__AppDir__%findstr.exe" /IRC:"^\"[;\ \ ]*%ISKey%[\ \ ]*=[\ \ 01]*\"$">NUL&&Echo(%ISKey%=1||Echo(%%$
)Else Echo(%%$)Else Echo(%%$
EndLocal)>>"%IFile%"
@Pause
由于您提供的信息很少,并且没有证据表明您甚至亲自尝试过此任务,因此,在此之后,我将不再提供进一步的支持。尽管下面值得一提的是"^\"[;\ \ ]*%ISKey%[\ \ ]*=[\ \ 01]*\"$"
实际上是:“ ^ \ ” [ ; \ SPACE \ TAB ] * % I S K e y % [ \ SPACE \ TAB ] * = [ \ SPACE \ TAB 0 1 ] * \ “ $ ” 。