Batch file to extract lines of text from .ini

May 18, 2009 at 07:00:37
Specs: Windows XP
Hi. I have a text file "listfile.ini" with up to 50 entries all with the syntax:
[code]
; Begin ABC series
[ABC1]
PATH="..\..\..\ABC1\"
CMD="SetKey 11111-22222-33333-44444-55555"

[ABC2]
PATH="..\..\..\ABC2\"
CMD="SetKey 11111-22222-33333-44444-55555"
; End ABC series

; Begin DEF series
[DEF1]
PATH="..\..\..\DEF1\"
CMD="SetKey 11111-22222-33333-44444-55555"

[DEF2]
PATH="..\..\..\DEF2\"
CMD="SetKey 11111-22222-33333-44444-55555"
; End DEF series
[/code]

All the entries are laid out in this manner with the headings in brackets [xyz] and in sets preceded by a semi-colon (; Begin) and ending with a semi-colon (; End).
What I need is to set a string based on user input (I already have this code), then pass the string to a function that will copy the set of lines to another text file (ie. myfile.txt) so that on user input of ABC, the output of myfile.txt contains ONLY the following:
[code]
; Begin ABC series
[ABC1]
PATH="..\..\..\ABC1\"
CMD="SetKey 11111-22222-33333-44444-55555"

[ABC2]
PATH="..\..\..\ABC2\"
CMD="SetKey 11111-22222-33333-44444-55555"
; End ABC series
[/code]

I tried the code from this post:
http://www.computing.net/answers/pr...
But I only get the last line of text from listfile.ini displayed in the command window and the output of myfile.txt is blank.
This batch file would be expected to run in a Windows XP and DOS environments.
Any help is greatly appreciated.


See More: Batch file to extract lines of text from .ini

Report •


#1
May 18, 2009 at 07:38:25
if you are not restricted in any way regarding the use of tools/languages, here's an example in Python making use of modules specially designed to parse ini files like that
import ConfigParser
choice=raw_input("Enter your input( eg ABC): ").upper()
config = ConfigParser.ConfigParser()
config.read("file")
for section in config.sections():    
    if choice in section :
        print "[%s]" % section
        for options in config.options(section):
            print "%s = %s" % (options, config.get(section, options))
                

output
c:\test> type file
; Begin ABC series
[ABC1]
PATH="..\..\..\ABC1\"
CMD="SetKey 11111-22222-33333-44444-55555"

[ABC2]
PATH="..\..\..\ABC2\"
CMD="SetKey 11111-22222-33333-44444-55555"
; End ABC series

; Begin DEF series
[DEF1]
PATH="..\..\..\DEF1\"
CMD="SetKey 11111-22222-33333-44444-55555"

[DEF2]
PATH="..\..\..\DEF2\"
CMD="SetKey 11111-22222-33333-44444-55555"
; End DEF series

c:\test> python myscript.py
Enter your input( eg ABC): abc
[ABC1]
cmd = "SetKey 11111-22222-33333-44444-55555"
path = "..\..\..\ABC1\"
[ABC2]
cmd = "SetKey 11111-22222-33333-44444-55555"
path = "..\..\..\ABC2\"



Report •

#2
May 18, 2009 at 08:15:41
I'd much prefer batch programming since this functionality will be added to an already existing complex batch file.
I'm not familiar with python. Can it be called from a batch file? And can it be run in a 16-bit environment?
Thanks. You guys are quick!

Report •

#3
May 20, 2009 at 00:42:44
Just to clarify a few items. I need batch code to add to an existing batch file to accomplish the task in the first post. Sorry for the restriction, but this must be in batch code due to the 16-bit DOS operating environment requirement.
Also, the "listfile.ini" source file may have any number of single or multiple entries within the framing comments (; Begin... ; End...) so ideally the matching/copying function should use those framing statements to capture/extract the text. Copying the framing comments to the new myfile.txt is prefered but not required.

I'm sure the code I linked to in the first post will work, but I'm screwing up something in the syntax. I'll keep plugging away at it until someone more clever than me figures out an elegant solution.

I could probably get away with autoit, but I'd have to rewrite my current functional batch file into AutoIt. As cool as Python is, I can't use it.


Report •

Related Solutions

#4
June 24, 2009 at 00:40:00
So there are no other suggestions? Thanks for the suggestion so far, but I can't use Python. It must be in batch code.

Report •

#5
June 24, 2009 at 05:28:38
The level of text parsing required for .ini files was not possible from batch files until WinNT, which is a 32-bit/64-bit OS.

Report •


Ask Question