Read Different Lines Into Different Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Read Different Lines Into Different Variable

#1 Post by phillid » 03 May 2010 01:53

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

1+2=46
Posts: 25
Joined: 23 Mar 2010 14:19

Re: Read Different Lines Into Different Variable

#2 Post by 1+2=46 » 03 May 2010 05:03

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Read Different Lines Into Different Variable

#3 Post by aGerman » 03 May 2010 05:16

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Read Different Lines Into Different Variable

#4 Post by avery_larry » 03 May 2010 09:44

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 . . .

Post Reply