Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is it possible to make a batch file take a key/string from an .ini file to use as a variable? And also how would i get a batch file write a variable to a .txt and a .ini file(not both types at once though)?

You neeed t know the structure of the girst file.
=====================================
If at first you don't succeed, you're about average.M2

@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (test) do (
set /a N+=1
set v!N!=%%a
)
echo these !N! bars were set
set v
=====================================
If at first you don't succeed, you're about average.M2

@echo off
:: ScanIni.cmd -- scan .ini filesif not "%2" == "" if "%3" == "" goto :start
echo.
echo Usage: ScanIni ini_file section
echo Sets variables named in specified section of ini_file to their values
echo Also sets VARIABLES to comma-separated list of variables found
echo.
echo Example: suppose you run ScanIni test.ini test2
echo and the file test.ini contains the following:
echo.
echo [test1]
echo this=ignored - wrong section
echo.
echo [test2]
echo ; Comments skipped
echo ThisOne=Scanned ; Comment
echo SoIs=This one too!
echo.
echo Then you will have the following variables set:
echo VARIABLES=ThisOne,SoIs
echo ThisOne=Scanned
echo SoIs=This one too!
exit /b 1:start
set VARIABLES=
set __ScanIni_inSection__=
for /f "tokens=1* eol=; delims==" %%x in (%1) do (
if "%%y" == "" (
if "%%x" == "[%2]" (
set __ScanIni_inSection__=Yes
) else (
set __ScanIni_inSection__=
)
) else if defined __ScanIni_inSection__ (
set %%x=%%y
call :addVariable %%x
)
)
set __ScanIni_inSection__=
exit /b:addVariable
if "%VARIABLES%" == "" (
set VARIABLES=%1
) else (
set VARIABLES=%VARIABLES%,%1
)
exit /b

Mechanix2Go:
Your code does not work like i think it should, but i can use it to read the lines of any file but not a properly formatted ini file.

since that's the case , I would suggest NOT to use batch for parsing ini files. Its tedious and cumbersome. You can use more advance string features from languages such as Perl/Python/Ruby etc, or use modules provided by these languages that deals with such tasks.Here's an example in Python using ConfigParser module.
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("file")
print "First item from [test] is : " ,config.get("test", "T1")
print "Second item from [test] is : ", config.get("test", "T2")
print "Third item from [test] is : ",config.get("test", "T3")
save the above as script.py and on command line:
c:\test> python test.py
First item from [test] is : Hi
Second item from [test] is : Working
Third item from [test] is : Success

So a 'properly formatted' ini file is what?
=====================================
If at first you don't succeed, you're about average.M2

M2: i mean it does not take the t1= part of the line or the part in the []. it just needs to take after the = sign.

Hi,
Mmm. Not altogether sure what you are trying to achieve.
The following script should echo all property values in the "my.ini" file:
@echo off
setlocal enabledelayedexpansion:init_vars
:: this is our ini file path
set ini_file=my.ini
goto show_values:show_values
:: this reads the first two 'tokens' of every line
for /F "tokens=1,2 delims==" %%L in (%ini_file%) do (
if not [%%M]==[] (
echo Value: %%M
)
)
goto eof:eof
echo Press any key to quit
pause > NULhth?
Elinor
Elinor Hurst
http://elinorhurst.blogspot.com

![]() |
![]() |
![]() |

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