Computing.Net > Forums > Programming > Batch file to extract lines of text from .ini

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to extract lines of text from .ini

Reply to Message Icon

Name: mr_smartepants
Date: May 18, 2009 at 07:00:37 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 18, 2009 at 07:38:25 Pacific
Reply:

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\"



0

Response Number 2
Name: mr_smartepants
Date: May 18, 2009 at 08:15:41 Pacific
Reply:

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!


0

Response Number 3
Name: mr_smartepants
Date: May 20, 2009 at 00:42:44 Pacific
Reply:

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.


0

Response Number 4
Name: mr_smartepants
Date: June 24, 2009 at 00:40:00 Pacific
Reply:

So there are no other suggestions? Thanks for the suggestion so far, but I can't use Python. It must be in batch code.


0

Response Number 5
Name: Razor2.3
Date: June 24, 2009 at 05:28:38 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to extract lines of text from .ini

Batch File to Extract data www.computing.net/answers/programming/batch-file-to-extract-data/15330.html

Batch file to extract certain lines www.computing.net/answers/programming/batch-file-to-extract-certain-lines/17465.html

Extract First Line of Text www.computing.net/answers/programming/extract-first-line-of-text/13939.html