Batch file to edit an .ini

本小妞迷上赌 提交于 2020-01-24 01:41:06

问题


I have a program for which a setting in the .ini file seems to constantly revert. I find myself constantly checking the file to see if it needs to be edited, so I would like to see if I can come up with a batch file that will do this job. The idea is to create this batch file to scan the .ini file every 2 minutes to check the value of a particular line and change the value if necessary. The line is:

UpdateSpeedCore=8

8 is the desired number for the check, but it sometimes reverts to 100.

The name of the file is prolasso.ini and the path is C:\Documents and Settings\Administrator\Application Data\ProcessLasso\config\prolasso.ini.

Thankyou to anyone who can help with this annoyance...

Edit: More on the .ini file. There are no empty lines. However, there are some lines that are set "=" to no value like "Power=". There are maybe half a dozen section delineators in the file like "[Debug]" or "[AdvancedRules]". These are not set equal to a value. It's a static length lines wise and about 100 lines long. Other than the section delineators, all the lines use an "=" sign followed by a value. These are preceded by the setting name as in "UpdateSpeedCore".


回答1:


This expects the UpdateSpeedCheck=8 to be on a line by itself with no spaces.

It uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855 which you can put in the same folder.

@echo off
set "file=C:\Documents and Settings\Administrator\Application Data\ProcessLasso\config\prolasso.ini"

:loop
findstr "^UpdateSpeedCheck=8$" "%file%" >nul || (
type "%file%"|repl "^UpdateSpeedCheck=.*" "UpdateSpeedCheck=8" >"%file%.tmp"
move "%file%.tmp" "%file%" >nul
)
ping -n 120 localhost >nul
goto :loop



回答2:


@echo off
echo. >prolasso.new
FOR /F "delims=\= tokens=1,2" %%K IN (prolasso.ini) DO (
    IF "%%K" NEQ "UpdateSpeedCheck" (
        >>prolasso.new echo %%K=%%L
    ) else (
        >>prolasso.new echo %%K=8
    )
)
del prolasso.ini
ren prolasso.new prolasso.ini

note: this solution will delete empty lines.

edit: solved the problem with the additional space on every run (take care, that there is no space after echo %%K=%%L). This should also solve the problem with some-thousand-runs (probably because of the big line-length)




回答3:


http://www.pixelbeat.org/programs/crudini should work on all platforms and can be used to read and update the ini file in a robust and simple way.

crudini --set prolasso.ini '' UpdateSpeedCore 8

Putting it in a loop:

:loop
crudini --set prolasso.ini '' UpdateSpeedCore 8
ping localhost -n 121 > nul
goto loop



回答4:


On Windows you can use IniFile to manipulate the contents of .ini files from batch scripts. IniFile operations are idempotent.

inifile.exe "C:\Documents and Settings\Administrator\Application Data\ProcessLasso\config\prolasso.ini" UpdateSpeedCore=8


来源:https://stackoverflow.com/questions/18871870/batch-file-to-edit-an-ini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!