Page 1 of 1 1
Topic Options
#125244 - 2004-08-18 04:09 PM Read Windows Variables From INI
Austin Offline
Seasoned Scripter

Registered: 2002-01-24
Posts: 439
When I read entries from an INI file such as %UserProfile% I seem to be having trouble getting those to function inside the code.. such as

Code:
 
$Test = ReadProfileString("C:\file.ini","Section1","WinVar")


Where C:\file.ini is
[SECTION1]
WinVar=%USERPROFILE%

But if I say Code:
?"Var= "%USERPROFILE%   


it will work...

Is there any way to read these dynamic type things from ini file? Thanks in advance for the help.
_________________________
Austin Henderson

Top
#125245 - 2004-08-18 04:13 PM Re: Read Windows Variables From INI
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You need to expand the variable.

One simple method is:
Code:
$Test = ReadProfileString("C:\file.ini","Section1","WinVar")

$=Execute('$$Test="$Test"')
"Variable is now "+$Test+@CRLF



Note, this example requires that NoVarsInStrings is *not* ON.

Top
#125246 - 2004-08-18 04:34 PM Re: Read Windows Variables From INI
Austin Offline
Seasoned Scripter

Registered: 2002-01-24
Posts: 439
What is NoVarsInStrings ? I dont believe I have seen that one before.. and a search of the CHM file doesnt turn anything up either.

Thanks for the help
_________________________
Austin Henderson

Top
#125247 - 2004-08-18 04:40 PM Re: Read Windows Variables From INI
Austin Offline
Seasoned Scripter

Registered: 2002-01-24
Posts: 439
Ahh.. got it

Code:
 
$ItemPath = ReadProfileString("$INIPath",$Section,"ItemPath")
$Silent = Execute('$$ItemPath = "$ItemPath"')



Where the ini entry looks like..
ItemPath=%USERPROFILE%\Start Menu\Test.exe

Thanks for your help.. I dont think I ever knew about execute() either.. always something new to learn.
_________________________
Austin Henderson

Top
#125248 - 2004-08-18 04:51 PM Re: Read Windows Variables From INI
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you don't need execute either.
you also have expandenvironmentvars()

don't find it in chm?
you sure you have new one?
if you have, throw it to recycle bin and open the kixtart-bible (the word document that comes with kixtart)
_________________________
!

download KiXnet

Top
#125249 - 2004-08-18 04:53 PM Re: Read Windows Variables From INI
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Look under "SetOption()" for NoVarsInStrings and similar.

For good coding practice you would normally set NoVarsInStrings and Explicit ON.

Another way to expand the variable is to use ExpandEnvironmentVars().

In your case you could shorten the code to:
Code:
$ItemPath=ExpandEnvironmentVars(ReadProfileString("$INIPath",$Section,"ItemPath"))



So, why did I show you the Execute() method?

Well, Execute() is very useful for many tricks like this - variable variable names are another one, so it's worth learning.

Another benefit of using Execute() is that you can include KiXtart macros and variables in the ini file as well as environment variables. In fact your ini file could contain mini KiXtart scripts!

Top
#125250 - 2004-08-18 05:45 PM Re: Read Windows Variables From INI
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Quote:


So, why did I show you the Execute() method?

Well, Execute() is very useful for many tricks like this - variable variable names are another one, so it's worth learning.

Another benefit of using Execute() is that you can include KiXtart macros and variables in the ini file as well as environment variables. In fact your ini file could contain mini KiXtart scripts!




yeah, execute is a nice one to learn. I used it for a program that does just what richard said... my ini file contained miny kix scripts and kix commands.

also, it plays a huge role in kixforms

Bryce

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.06 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org