Solved

Execute vbscript variable as code

Posted on 2013-12-20
5
431 Views
Last Modified: 2013-12-20
Experts,

This should be relatively straight forward, but I'm drawing a blank.

I'm trying to execute some dynamic code in vbscript from a variable, but I can't seem to get it right:

Sub subAddDic(strDicObj, strDicKey, strDicValue)
	If strDicObj <> "" And strDicKey <> "" And strDicValue <> "" Then
		Set WshShell = CreateObject("WScript.Shell")
		strAddCmd = _
			"If Not " & strDicObj & ".Exists(" & fncQuote(strDicKey) & ") Then" & vbCrLf & _
				strDicObj & ".Add " & fncQuote(strDicKey) & " " & fncQuote(strDicValue) & vbCrLf & _
			"End If"
		WshShell.Run(strAddCmd)
		Set WshShell = Nothing
	End If
End Sub

Function fncQuote(strData)
	If strData <> "" Then
		fncQuote = Chr(34) & strData & Chr(34)
	Else
		fncQuote = ""
	End If
End Function

Open in new window


I'm erroring on this line "WshShell.Run(strAddCmd)" - where it's attempting to look for a file on the filesystem, and tells me that it's not found.

Anyone know how I can execute the inline variable as just vbs?
0
Comment
Question by:usslindstrom
  • 3
  • 2
5 Comments
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 500 total points
ID: 39731782
Obviously, WshShell.Run runs an executable file on the OS ;-)
So it tries to run "If"...
0
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 500 total points
ID: 39731784
0
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 39731842
That did it, thanks guys.

The 'Execute' method works great.  I noticted I was missing a comma in my previous script, which could have been causing me a few headaches too.

Not much different, but here's the working version if anyone's interested:

Sub subAddDic(strDicObj, strDicKey, strDicValue)
	If strDicObj <> "" And strDicKey <> "" And strDicValue <> "" Then
		strAddCmd = _
			"If Not " & strDicObj & ".Exists(" & fncQuote(strDicKey) & ") Then" & vbCrLf & _
				strDicObj & ".Add " & fncQuote(strDicKey) & ", " & fncQuote(strDicValue) & vbCrLf & _
			"End If"
		Execute strAddCmd
	End If
End Sub

Function fncQuote(strData)
	If strData <> "" Then
		fncQuote = Chr(34) & strData & Chr(34)
	Else
		fncQuote = ""
	End If
End Function

Open in new window


Much appreciated - you guys rock!
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39731921
Actually I am just ONE guy, but anyways...
Thanks a lot for sharing your solution with the EE community ;-)
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 39731955
lol - I just noticed that it was you that answered twice.  Apologies, I wasn't paying attention to the usernames.
0

Join & Write a Comment

Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now