Batch to read n-1 line from an .ini file

January 25, 2011 at 22:45:53
Specs: Windows XP
Hello experts,
I'm quite new to batch command and I really need your help. I have a Config.ini file, at the end of every group or [bracket] it always separated by an empty line like this:

------------- Begin of Config.ini
[General]
VerboseMode=0

[List0]
Name=Bart
Available=1
Folder=Characters/Bart.folder
Default=1

[List1]
Name=Homer
Available=1
Folder=Characters/Homer.folder

[List2]
Name=Lisa
Available=1
Folder=Characters/Lisa.folder

------------- End of Config.ini (19 lines)

If a list is active, it will be marked by "Default" variable followed by "1" string. Otherwise it will be left empty without "Default" variable and also without "1" string.
This Config.ini only have one "Default" variable, it could move to another list if switched through main application.

Anyway, in this case the "Default=1" is n, and will always be. I need a batch file which could echo variable value only from n-1 line which is "Bart.folder" string.

Here is my attempt:

------------- Begin of DeFolder.bat
@echo off
setLocal EnableDelayedExpansion
cls
for /f "tokens=1 delims=:" %%A in ('type Config.ini ^| findstr /r /b /i /n /c:"Default=1"') do (
set /a A=%%A
set /a B=%%A%-1
)
echo Default variable / n is line: %A%
echo Folder variable / n-1 is line: %B%
echo -----
echo "I wish I could echo variable value from line 7 here. Unexpected output when batch below is executed, no more empty line separators."
echo -----

for /f "tokens=* delims= " %%C in (Config.ini) do (
set /a n+=1
set v!n!=%%C
)
set line1=!v1!
echo Line1: %line1%
set line2=!v2!
echo Line2: %line2%
set line3=!v3!
echo Line3: %line3%
set line4=!v4!
echo Line4: %line4%
set line5=!v5!
echo Line5: %line5%
set line6=!v6!
echo Line6: %line6%
set line7=!v7!
echo Line7: %line7%
set line8=!v8!
echo Line8: %line8%
set line9=!v9!
echo Line9: %line9%
set line10=!v10!
echo Line10: %line10%
set line11=!v11!
echo Line11: %line11%
set line12=!v12!
echo Line12: %line12%
set line13=!v13!
echo Line13: %line13%
set line14=!v14!
echo Line14: %line14%
set line15=!v15!
echo Line15: %line15%
set line16=!v16!
echo Line16: %line16%
set line17=!v17!
echo Line17: %line17%
set line18=!v18!
echo Line18: %line18%
set line19=!v19!
echo Line19: %line19%
echo -----
echo "Output above doesn't contain any empty line separators. So n-1 formula is not applicable anymore. Why?"
echo -----

for /f "tokens=2 delims==" %%D in ('type Config.ini ^| findstr /r /b /i /n /c:"Folder="') do (
echo Folder: %%~nxD
)
echo -----
echo "All I need is output like this, but from 7th line only. Just "Bart.folder", not all."
echo -----
------------- End of DeFolder.bat

I spend countless hours doing it but I cannot seem to find anything. If anyone can help, it would greatly be appreciated. Thanks in advance.


See More: Batch to read n-1 line from an .ini file

Report •


#1
January 26, 2011 at 00:08:32
:: ==========================================
::
:: acklee.bat Wed 26-01-2011 14:36:50.32
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "Default" ^< config.ini') do (
set D=%%a
)
set /a F=D-1

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< config.ini') do (
if %%a equ !F! set %%b
)
echo.!Folder!


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
January 26, 2011 at 01:14:59
Wow!!
M2, thanks for your help, this is awesome & I appreciate it.
So, it's my amended:

@echo off & setLocal enableDELAYedeXpansion
cls
for /f "tokens=1 delims=[]" %%a in ('find /n "Default" ^< config.ini') do (
set D=%%a
)
set /a F=D-1

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< config.ini') do (
if %%a equ !F! set %%b
)

for /f "tokens=2 delims=/" %%c in ('echo !Folder! ^| find "Characters/"') do (
echo %%c
)

Thank you :)


Report •

#3
January 26, 2011 at 01:26:46
You don't need to FIND.

:: ==========================================
::
:: acklee.bat Wed 26-01-2011 14:36:50.32
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "Default" ^< config.ini') do (
set D=%%a
)
set /a F=D-1

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< config.ini') do (
if %%a equ !F! set %%b
)
echo.!Folder!

for /f "tokens=2 delims=/" %%a in ("!Folder!") do (
set var=%%a
)
echo.!var!


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
January 26, 2011 at 02:50:09
as usual, don't use batch for parsing files. Its slow and ugly. In the above batch, you are opening the file twice. Why not do it in just one pass?
If you can, use a file parsing tool or a proper language. Here's how you can do it efficiently with gawk for windows

C:\test>gawk -F"=" "/Default/{print p; m=split(p,a,/\//);print a[m] }{p=$2}" config.ini
Characters/Bart.folder
Bart.folder


that's all you need. No need for clumsy batch.

GNU win32 packages | Gawk


Report •

#5
January 26, 2011 at 04:06:16
No doubt unix-like tools are better suited. But at least half the time, the requestor can't or won't use them.


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
January 26, 2011 at 05:23:38
and half the time, the requester doesn't know better. :)
even if half the time requester cannot use it due to some dumb reasons, a vbscript is still better than batch.

GNU win32 packages | Gawk


Report •

#7
January 29, 2011 at 23:06:30
Hello ghostdog,
I know you're better at least than me, but let me tell you one of my dumb reasons:

All I need is output from batch algorithm. I'm not talking or even request about any other script here. Doesn't matter for me how it processed, even double, triple or quadruple times slower. I don't think it could harm my processor or memory anyway. Just to make it more portable, usable in any available "MS Dirty Operating System", without dependencies to any 3rd party programs.

I owe you big for your gawk knowledge..


Report •

Ask Question