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
Read Different Lines Into Different Variable
Moderator: DosItHelp
Re: Read Different Lines Into Different Variable
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
Another possibility could be this:
my.ini
my.bat
regards
aGerman
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: Read Different Lines Into Different Variable
Similar to others, here's what I did, allowing for rem and :: to be skipped:
with sa.ini looking like:
Could probably add another findstr command for "[" if you want to have sections inside square brackets . . .
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 . . .