Computing.Net > Forums > Programming > Save an ini file as its first line

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.

Save an ini file as its first line

Reply to Message Icon

Name: Ryanoc3ros
Date: May 8, 2009 at 22:51:37 Pacific
OS: Windows Vista
Subcategory: Batch
Comment:

I have a ini file containing the following information.

[Meter1]
variable_1=Hello
variable_2=World
variable_3=Goodbye

Is there a way to extract the value of the fist variable (Hello), and save the the entire file as "Hello.txt"?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 8, 2009 at 23:07:16 Pacific
Reply:

:: extract value of first ver in my.ini and rename ini
:: exthello Sat 09-05-2009 12:59:36.85

@echo off & setLocal EnableDelayedExpansion

for /f "tokens=2 delims==" %%a in ('find "variable" ^< my.ini') do (
if not defined new set new=%%a
)

echo ren my.ini !new!.txt


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Ryanoc3ros
Date: May 8, 2009 at 23:42:01 Pacific
Reply:

Thanks a lot! This works good for when the variable is just one word. I cant seem to figure out how to properly rename a file if the variable is equal to two words with a space.


0

Response Number 3
Name: Ryanoc3ros
Date: May 8, 2009 at 23:45:52 Pacific
Reply:

Nevermind, I got it. Thanks for your help!


0

Response Number 4
Name: Mechanix2Go
Date: May 9, 2009 at 00:09:42 Pacific
Reply:

@echo off & setLocal EnableDelayedExpansion

for /f "tokens=2 delims==" %%a in ('find "variable" ^< my.ini') do (
if not defined new set new=%%a
)

echo ren my.ini "!new!.txt"


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: ghostdog
Date: May 9, 2009 at 00:22:42 Pacific
Reply:

if you find yourself parsing ini files often (or not) , its ok to use an ini parser, such as those from Perl, or Python (and others)
an eg done with Python

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("file")
for section in config.sections():    
    if section == "Meter1":
        if "variable_1" in config.options(section):
            print config.get(section, "variable_1")

output:
c:\test> more file
[Meter1]
variable_1=Hello world
variable_2=World
variable_3=Goodbye
c:\test> python test.py
Hello world



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: Save an ini file as its first line

Inserting Lines to an INI file www.computing.net/answers/programming/inserting-lines-to-an-ini-file/17678.html

prevent saving an HTML file with im www.computing.net/answers/programming/prevent-saving-an-html-file-with-im/9321.html

Editing .ini file with a batch file www.computing.net/answers/programming/editing-ini-file-with-a-batch-file/12127.html