Computing.Net > Forums > Programming > Batch file variables

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 variables

Reply to Message Icon

Name: RMDan
Date: August 6, 2008 at 12:23:20 Pacific
OS: Xp
CPU/Ram: ?
Product: Gateway
Comment:

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)?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 6, 2008 at 12:45:46 Pacific
Reply:

You neeed t know the structure of the girst file.


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

M2


0

Response Number 2
Name: RMDan
Date: August 6, 2008 at 13:08:54 Pacific
Reply:

lets say the ini file is:

[test]
T1=Hi
T2=Working
T3=Success

How would i get each string as a variable?


0

Response Number 3
Name: Mechanix2Go
Date: August 6, 2008 at 14:45:49 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /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


0

Response Number 4
Name: ghostdog
Date: August 6, 2008 at 20:31:14 Pacific
Reply:

and after you get those strings to variables, what do you want to do next?


0

Response Number 5
Name: klint
Date: August 7, 2008 at 07:52:21 Pacific
Reply:


@echo off
:: ScanIni.cmd -- scan .ini files

if 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



0

Related Posts

See More



Response Number 6
Name: RMDan
Date: August 7, 2008 at 17:42:44 Pacific
Reply:

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.


0

Response Number 7
Name: ghostdog
Date: August 7, 2008 at 18:07:11 Pacific
Reply:

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


0

Response Number 8
Name: Mechanix2Go
Date: August 7, 2008 at 19:37:20 Pacific
Reply:

So a 'properly formatted' ini file is what?


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

M2


0

Response Number 9
Name: RMDan
Date: August 9, 2008 at 21:34:44 Pacific
Reply:

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.


0

Response Number 10
Name: Elinor
Date: August 10, 2008 at 05:18:05 Pacific
Reply:

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 > NUL

hth?

Elinor

Elinor Hurst
http://elinorhurst.blogspot.com


0

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 variables

Batch file variable creation www.computing.net/answers/programming/batch-file-variable-creation/14564.html

batch file variable reset www.computing.net/answers/programming/batch-file-variable-reset/16220.html

Batch File Variable Help www.computing.net/answers/programming/batch-file-variable-help/17718.html