2

I have a vbscript that will read in commands from a text file. I am thinking of using the Execute and Eval commands to execute the commands from the file. Obviously, I will read in the file as a string, and parse the string into Eval and Execute commands. However, I am not sure if this is possible, and how to go about doing it?

For example, let's say I have the following file:

x = 3
y = 4
z = x + y

Print z

In my vbscript, I want something like the following:

' Read in file to string
' Split the string at the newline (commandList)
For Each str in commandList:
    Execute(str)
Next str

The output would be:

7

I have tried the following to get a proof of concept:

Execute("x = 1")

However, I keep getting a "Function terminated during evaluation" error. Not sure what I am doing wrong. I am not too familiar with vbscript.

2
  • 1
    Are you sure your question is on vbscript? Please edit your question and show your text file containing commands to be executed and an adequate code snippet (refer to How to Ask). Commented Apr 24, 2015 at 18:36
  • Can't reproduce "Function terminated during evaluation" error (using cscript.exe) so probably something other than a mistake in Execute("x = 1"). Tried changing one of the spaces to a non-breaking space (U+00A0), got "Syntax Error". Tried defining a function/sub x(), got "Illegal assignment". Tried presetting x to an object, just overwrote it with the value. I suspect a clue might be in the sample input. Which host environment provides a "print" statement? Perhaps it is the environment that doesn't like the Execute statement. Commented Sep 4, 2020 at 23:10

2 Answers 2

1

I think the best explanation is here: Link-1 In short, Eval() command takes an expression and returns its value, while Execute() command takes a group of statements and executes them (in global scope I think). In general, Eval() expects some result of expression, while Execute() doesn't. For detailed explanation see the link above. Paul

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

2 Comments

1) Eval doesn't expect the result of an expression. It expects a string representation of an expression and returns the result of evaluating that expression. Similarly, Execute[Global] expects a string representation of a statement list and executes it (them). Note that, in VBScript, an expression by itself is NOT a statement and cannot be evaluated by Execute[Global]. See this answer for a detailed explanation.
2) Execute runs in local scope, ExecuteGlobal runs in global scope.
0

Your concept seems to be right.

==>type D:\VB_scripts\SO\29832173.vbs
option explicit
Dim commandList(2), str
commandList(0)= "x = 3"
commandList(1)= "y = 4"
commandList(2)= "z = x + y"
For Each str in commandList
  Execute str
Next
Wscript.echo x, y, z

==>cscript D:\VB_scripts\SO\29832173.vbs
3 4 7

==>

Explanation (Execute vs. Eval):

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, the result is True; if they are not, the result is False. The Eval method always uses the second interpretation, whereas the Execute statement always uses the first.

Edit with regard to OP's comment: This is giving me a type mismatched error when getting in the For loop. With some additional debugging echo and with For Each loop changed to For...Next iteration:

==>type D:\VB_scripts\SO\29832173.vbs
option explicit
Dim commandList(2), str, ii
commandList(0)= "x = 3"
commandList(1)= "y = 4"
commandList(2)= "z = x + y"
For ii = 0 To UBound( commandList)
  Wscript.Echo "To execute: " & commandList( ii), CStr( Eval( commandList( ii)))
  Execute commandList( ii)
  Wscript.Echo "  Executed: " & commandList( ii), CStr( Eval( commandList( ii)))
Next
Wscript.Echo x, y, z

==>cscript D:\VB_scripts\SO\29832173.vbs
To execute: x = 3 False
  Executed: x = 3 True
To execute: y = 4 False
  Executed: y = 4 True
To execute: z = x + y False
  Executed: z = x + y True
3 4 7

==>

2 Comments

This is giving me a type mismatched error when getting in the For loop.
It is still telling me the "Function terminated during evaluation" on the Execute line. If I add triple quotes around the commands ("""COMMAND"""), I do not get the error, but nothing prints out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.