Save an ini file as its first line

May 8, 2009 at 22:51:37
Specs: Windows Vista
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"?


See More: Save an ini file as its first line

Report •


#1
May 8, 2009 at 23:07:16
:: 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


Report •

#2
May 8, 2009 at 23:42:01
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.

Report •

#3
May 8, 2009 at 23:45:52
Nevermind, I got it. Thanks for your help!

Report •

Related Solutions

#4
May 9, 2009 at 00:09:42
@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


Report •

#5
May 9, 2009 at 00:22:42
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



Report •


Ask Question