Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript method to show all variable values

Status
Not open for further replies.

ak02

Technical User
Oct 3, 2006
3
US
Hi:

Does anyone know of a method by which a script can return the value of all variables that are currently active in it? I am writing a log sub and am trying to have a routine by which whenever the log sub is called, it will dump the values of all variables in the script to it (the log itself is a text file). So far, I haven't been able to find a collection or function that accomplishes this.

While it is obviously simple to do a simple write_file.WriteLine "variablename = " & variablename, I am trying to avoid having to explicitly list each one on it's own string and am trying to find a method to just "dump" all of them to a text file.

Thanks,

Alan
 
If you are doing this against a vbs file then yes. But the best way i can think of is to parse the file grabing the declarations. That leaves conditioning for inside functions/sub ect though. They technically are not in use and using memory unless the function is called and thus creating them.

Are you more looking to test how much memory the vbs is using? That is a far easier task which esentially tells you the same thing but in the form of what the machine cares about in memory usage which is all your variables etc..


____________ signature below ______________
General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
There are several things here

1) Global variables
2) Stack based variables
3) Class member variables
4) Simple variables (eg integer, string etc)
5) Complex variables (eg FileSystemObject, user defined classes)
6) Variables "included" by ExecuteGlobal

You need to have a method of printing/navigating the complex variables and they can be very complex when it comes to collections.

You also need a method of accessing private class member variables, which, by definition, are private and cannot be accessed by outsiders.
 
Thanks. I am interested in getting the values for the global and local (within sub) variables in my script. I just need a method by which I can dump all the variable names and their associated values "on-demand" such as when I want to log information to a log file. The intent is to do this while the script is running rather than debugging, etc... Essentially, I am just trying to avoid having to explicitly write code for each variable in use. I'm trying to code a method by which all the variables (without having to enumerate them) will be outputted with their respective values. Do you have any code snippets that illustrate the code required to do this?
 
Something simple would be to pass the variables you're interested in in an array and then just go through the array concatenating the variables to a string and printing them out.

Alternatively, use MSE which comes with Office. It is great for doing such things. Just drag whatever you're interested in into the watch window.
 
Hi:
Yes, I tried that already. Basically what I did is created a variable and set it equal to a string that contained the list of variables in the subroutine, each separated by a comma. I then used the Split function to break each part of that string into an array. The problem is that while I can loop through the array and display the variable names, I can't seem to code something that will display their values.

Sample:
varList="variable1, variable2, variable3, variable4"
arrVar=split(varList,",")
For count=0 to ubound(arrVar)
wscript.echo arrVar(count) & _______________
Next

The ______________ line indicates a place where I have tried a myriad of lines of code to get it to echo the value instead of the string. It just doesn't do it. Do you have a means by which to convert the string (arrVar(count) back into a variable so that it actually echo's the value of the variable instead of the name of it?
 
It is slightly more involved than that. You cannot do without taking into account of variable types. For simple types, eval() would do. (Else, I just display it typename.)
[tt]
For count=0 to ubound(arrVar)
'wscript.echo arrVar(count) & _______________
select case vartype(eval(arrVar(count)))
case vbEmpty, vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDate, vbString, vbBoolean, vbDecimal
wscript.echo count & vbcrlf & arrVar(count) & vbcrlf & eval(arrVar(count))
case else
wscript.echo count & vbcrlf & arrVar(count) & vbcrlf & typename(eval(arrVar(count)))
end select
Next
[/tt]
 
there is the Exec statement (i think its called that, its the one which allows you to excute sudo vbscript code from within vbscript, all very confusing) which may help with the output to screen,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top