Вопрос

I'm trying to make changes to an existing ini file and then rewrite those changes to a new ini file. The problem I am having is parsing the sections doesn't seem to work. If I use the following code:

set reso=%2
set RESLINE=RESMAX=0.72
set nRESLINE=RESMAX=%reso%

setlocal enableDelayedExpansion
(
for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
    set "lines=%%a"
    if defined lines set "lines=!lines:%p4p%=%new%!"
    if defined lines set "lines=!lines:%RESLINE%=%nRESLINE%!"
    echo(!lines!
)
) > new.ini

I get out a new.ini file which when the %p4p% variable is found in line, it replaces it with %new% which is fine. But when the RESLINE hits my output shows:

0.72=RESMAX=0.70=0.72

instead of:

RESMAX=0.70

as desired. I suspect this is because of the "=" being present in the strings, but I'm not sure how to get around it.

What I've tried doing instead is the following:

setlocal enableDelayedExpansion
(
for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
    set "lines=%%a"
    if defined lines set "lines=!lines:%p4p%=%new%!"

    for /f "tokens=1,2 delims==" %%y in ('%lines%') do (
    if %%y==RESMAX (
        set "oRES=%%z"
        set "lines=!lines:%oRES%=%reso%!"
    )
    )
    echo(!lines!
)
) > test.ini

But it seems in this case that the nested for doesn't ever read in the right information. I assumed what I was attempting here was to parse the read in line which should be of the format PARAM=VALUE, then check if the PARAM is = the word RESMAX and if it is, assign its corresponding value to the var oRES and then set the read-in line to now say RESMAX=newvalue. I've tried replacing the %lines% with !lines! but that just gets me a series of errors saying the various ini params don't exist as acceptable programs/batchs. Any help would be much appreciated.

Thanks

Это было полезно?

Решение

set "reso=%2"
set "old_reso=0.72"
set "RESLINE=RESMAX=0.72"
set "nRESLINE=RESMAX=%reso%"

setlocal enableDelayedExpansion
(
    for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
        set "lines=%%a"
        if defined lines set "lines=!lines:%p4p%=%new%!"
        if defined lines ( 
          rem set "lines=!lines:%RESLINE%=%nRESLINE%!"
          echo !lines! | find /i "%RESLINE%" >nul 2>&1 && (
            set "lines=!lines:%old_reso%=%reso%!"
          )
         )
        (echo(!lines!)
    )

) > new.ini

Try this. Or check dbenham's REPLVAR (the problem is that you cant directly replace the = sign)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top