?
Solved

How to pass parameters in vbscript

Posted on 2008-10-09
8
Medium Priority
?
2,050 Views
Last Modified: 2012-06-27
Hi,
i want to write a stand alone vbscript that get 5 numbers and return 2 results, the sum and the multipling of this 5 numbers. i need to run this vbscript from java code and use the return results.
please help me with tha vbscript and the java code (include use the return parameters).
Thanks.
0
Comment
Question by:ExpandNetworks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22682172
Hi, the only way that I can think of to do this is to pass the five numbers to the VBScript, as well as another argument that specifies whether you want to sum or multiply those numbers.  VBScript is only capable of returning one integer value at a time.

Here is the VBS that will do the calculation for you.  The first parameter must be a string of "sum" or "multiply".  The rest of the parameters can be 1 or more integers.

Regards,

Rob.

If WScript.Arguments.Count > 1 Then
	strMode = WScript.Arguments.Item(0)
	If LCase(strMode) = LCase("sum") Then
		intTotal = 0
	ElseIf LCase(strMode) = LCase("multiply") Then
		intTotal = 1
	End If
	For intArg = 1 To WScript.Arguments.Count - 1
		If LCase(strMode) = LCase("sum") Then
			intTotal = intTotal + CInt(WScript.Arguments.Item(intArg))
		ElseIf LCase(strMode) = LCase("multiply") Then
			intTotal = intTotal * CInt(WScript.Arguments.Item(intArg))
		End If
	Next
	WScript.Quit(intTotal)
Else
	WScript.Echo "There were not enough arguments passed to this script."
End If

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22682180
And this is a VBS implementation of how to call that function......I'm not that familiar with JavaScript, but perhaps this will help you create a JavaScript version, if you know it....

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
strVBS = "C:\Temp\Scripts\Test1.vbs"
'strMode = "sum"
strMode = "multiply"
arrNumbers = Array(1,2,3,4,5)
 
strCommand = "wscript " & strVBS & " " & strMode
For Each intNum In arrNumbers
	strCommand = strCommand & " " & intNum
Next
 
strReturn = objShell.Run(strCommand, 1, True)
MsgBox strReturn

Open in new window

0
 

Author Comment

by:ExpandNetworks
ID: 22688115
It is possible to return a string or an array instad of only one integer?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 65

Expert Comment

by:RobSampson
ID: 22691392
No, not without writing that value to a file or environment variable for the other script to read from.

The wscript.quit method is only capable of returning a single integer value.

If you want the VBScript to write to a file, for the JavaScript to pick up later, than can be done....

Regards,

Rob.
0
 

Author Comment

by:ExpandNetworks
ID: 22694474
so,
how can i use the environment variable?
can you please show me an example?
Thanks
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1000 total points
ID: 22699100
Hi, well, again, I'm not sure about a JavaScript implementation, but in VBScript, you can use the following code.

In the links in the comments, there is a slight alternative to this method for accessing environment variables as well.

Regards,

Rob.
strEnvVarName = "MakeRuleReturnCode"
Set_Env_Var strEnvVarName, "0"
strVarValue = Get_Env_Var(strEnvVarName)
 
MsgBox strEnvVarName & " currently has a value of " & strVarValue
 
Sub Set_Env_Var(strVarName, strVarValue)
 
	' SOURCE: http://www.codecomments.com/archive299-2005-12-736281.html
	' HOW TO ECHO???: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/05426477-f6e4-4dcb-8101-3f7d979c8ed5.asp
	Dim objShell, objEnvVar
	Set objShell = CreateObject("WScript.Shell")
	Set objEnvVar = objShell.Environment("Volatile")
	objEnvVar(strVarName) = strVarValue
	Set objEnvVar = Nothing
	Set objShell = Nothing
	
End Sub
 
'*************************************************************
 
Function Get_Env_Var(strVarName)
 
	' SOURCE: http://www.codecomments.com/archive299-2005-12-736281.html
	' HOW TO ECHO???: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/05426477-f6e4-4dcb-8101-3f7d979c8ed5.asp
	Dim objShell, objEnvVar
	Set objShell = CreateObject("WScript.Shell")
	Set objEnvVar = objShell.Environment("Volatile")
	Get_Env_Var = objEnvVar(strVarName)
	Set objEnvVar = Nothing
	Set objShell = Nothing
	
End Function

Open in new window

0
 

Author Closing Comment

by:ExpandNetworks
ID: 31504719
Thanks, Rob
It's was very helpfull.....
Thanks again.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22706867
No problem. Thanks for the grade. I hope you can find a JavaScript equivalent....

Rob.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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