Page 1 of 1

Read Different Lines Into Different Variable

Posted: 03 May 2010 01:53
by phillid
Hey. I'm making a program with a settings.ini file but I don't know how to read each line (which is a different setting) into their appropriate variables.
Thanks :D

Re: Read Different Lines Into Different Variable

Posted: 03 May 2010 05:03
by 1+2=46

Code: Select all

Set LINE=0
For /F "delims=" %%L in (settings.ini) do (
        Set /A LINE += 1
        If "![color=#FF0000]LINE[/color]!" == "4" (
                Set STR=%%L
                Echo.![color=#FF0000]STR[/color]:~7!> %TEMP%.\T1.DAT
                Findstr /I /B /C:" " %TEMP%.\T1.DAT > nul
                Del %TEMP%.\T1.DAT
         )
If /I !STR:~11!==[SETTING] set [VARIABLE]=[...]


STR is the column and LINE is the line. If you want line 3, you'll have to put line 2.

Example:
(In settings.ini)

[quote]Color=black
Color=white/quote]


The code would be (for line 2):

Code: Select all

Set LINE=0
For /F "delims=" %%L in (settings.ini) do (
        Set /A LINE += 1
        If "!LINE!" == "2" (
                Set STR=%%L
                Echo.!STR:~6!> %TEMP%.\T1.DAT
                Findstr /I /B /C:" " %TEMP%.\T1.DAT > nul
                Del %TEMP%.\T1.DAT
         )
If /I !STR:~6!==white set [VARIABLE]=[...]



Hope I helped.


BTW: I used this on my chat code, which you saw.

Re: Read Different Lines Into Different Variable

Posted: 03 May 2010 05:16
by aGerman
Another possibility could be this:

my.ini

Code: Select all

[general]
value1=a
value2=b



my.bat

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('findstr /r /v /c:"^\[.*\]$" "my.ini"') do set "%%a"
echo %value1%
echo %value2%
pause


regards
aGerman

Re: Read Different Lines Into Different Variable

Posted: 03 May 2010 09:44
by avery_larry
Similar to others, here's what I did, allowing for rem and :: to be skipped:

Code: Select all

for /f "tokens=*" %%a in ('type sa.ini^|findstr /v /i /l /c:"::"^|findstr /v /i /l /c:"rem "') do set %%a


with sa.ini looking like:

Code: Select all

rem This is my settings file
rem just put variable=value in here

yes=no
no=yes
up=down

::Bypass the common double colon as a comment
::and continue on
left=right


Could probably add another findstr command for "[" if you want to have sections inside square brackets . . .