1

I am trying to use vbscript's Eval (or maybe I need Execute) to create some variables from the key names from an ini file. The ini file can have unlimited unknown key=val pairs. I need to create a variable based on the key name no matter what.

Ini File contents:

myPath=c:\test
myExe=myapp.exe
....
xxx=123
yyy=abc

My code that reads the ini and returns the key and values to an object

The code I am trying to get working is here:

For each pair in objINI
    Eval("pair.key=pair.val")
Next

msgbox myPath
msgbox myExe

But both msgbox's are showing empty And yes I am sure pair.key and pair.val have the correct values.

Thoughts on what I am missing or if this is even possible?

3
  • 2
    I think you need to re-evaluate your architecture. Having an ini file that can set any variable it wants sounds...unsafe. Commented Jun 5, 2013 at 18:40
  • How it looks in my example isn't how it will actually work. There is no risk. Commented Jun 5, 2013 at 19:04
  • 2
    eval and risk are synonymous. Commented Jun 5, 2013 at 20:58

3 Answers 3

3

You need to Execute (an assign statement), not to Eval(uate a boolean expression):

>> n = "Name"
>> v = "Value"
>> WScript.Echo TypeName(Eval("n=v"))
>>
Boolean
>> Execute "n=v"
>> WScript.Echo n
>>
Value
>>

From the docs:

In VBScript, x = y can be interpreted two ways. The first is as an assignment statement, where the value of y is assigned to x. The second interpretation is as an expression that tests if x and y have the same value. If they do, result is True; if they are not, result is False. The Execute statement always uses the first interpretation, whereas the Eval method always uses the second.

(This does not mean you should do such things; neither at home, nor at work)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok thanks for clarifying.. I kept trying eval and execute but kept mixing up which was doing what. Got it working using this info and SLaks answer below
1

You eval'd the literal code pair.key = pair.value.
That assigns to pair.key.

You want to assign to the value of pair.key – if pair.key is myPath, you want to eval myPath = pair.value.
You can do that by concatenating strings:

Execute(pair.name + " = pair.value")

5 Comments

But I don't know that "myPath" will be my keyname. I need to generate the variables dynamically based on the key names from the file. My ini file might have "myPath".. another guy might call it "xPath" .. in the end, I will need to use msgbox myPath, and he will need to use msbox xPath
@Dss: Exactly. You need to concatenate the string to eval.
@SLaks -1 for mixing Eval and Execute[Global].
I don't quite get what you mean SLaks.. Concatenating strings doesn't seem to be relevant here. I am simply trying to convert a string into a variable. The pair.key is the full name of the variable-to-be. I think I need something like a Double Eval that will first eval the pair.key to a string, then eval that to be set as a variable to the pair.value
YES! Thanks ... I see what you mean now by concat. Working perfectly.
0

If you want to read key/value pairs from an INI file you'd be better off storing them in a dictionary. I wrote a function for this some years ago. Basically looks like this:

Function ParseIni(filename)
  Set ParseIni = Nothing

  Set config = CreateObject("Scripting.Dictionary")
  section = ""

  Set file = CreateObject("Scripting.FileSystemObject").OpenTextFile(filename)
  Do While Not file.AtEndOfStream
    line = Trim(Replace(file.ReadLine, vbTab, " "))
    If InStr(line, ";") > 0 Then line = Trim(Left(line, InStr(line, ";") - 1))
    If line <> "" Then
      If Left(line, 1) = "[" And Right(line, 1) = "]" Then
        ' line is a section name
        section = Trim(Mid(line, 2, Len(line) - 2))
        If section = "" Then _
          WScript.Echo "Parse Error: section name is empty string."
        If config.Exists(section) Then _
          WScript.Echo "Parse Error: duplicate section name '" & name & "'."
        config.Add section, CreateObject("Scripting.Dictionary")
      ElseIf InStr(line, "=") > 0 Then
        ' line is a parameter line
        If section = "" And Not config.Exists(section) Then _
          config.Add section, CreateObject("Scripting.Dictionary")
        param = Split(line, "=", 2)
        param(0) = Trim(param(0))
        param(1) = Trim(param(1))
        If param(0) = "" Then _
          WScript.Echo "Parse Error: invalid parameter name '" & param(0) & "'."
        If param(1) = "" Then param(1) = True
        config(section).Add param(0), param(1)
      Else
        ' line is neither parameter nor section name, thus invalid
        WScript.Echo "Parse Error: expected parameter definition in line '" _
          & line & "'."
      End If
    End If
  Loop
  file.Close

  Set ParseIni = config
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.