Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two files: batch.bat and vbscript.vbs. The .bat file contains a loop where I need to call the .vbs, passing to it 2 arguments. The .vbs file contains a function with 2 arguments( the arguments I am passing to the .vbs file). I need to have access in the .bat file to the value my function in .vbs returns.
Could someone help me, please?

I am not an expert so please excuse my poor syntax. I am calling the .bat file. The files should look something like this

batch.bat :

loop start

' calling the vbs file

cscript vbscript.vbs arg1 arg2 ( here I suppose something has to be add to get val from vbs)

' using value returned by .vbs function

loop end 

vbscript.vbs :

function myfunction(arg1,arg2)
dim value

' do some calculation

myfunction= value
end function

dim value_to_return_to_batch
' now calling the function
value_to_return_to_batch=myfunction(arg1,arg2)
'
' here something has to be add to send value_to_return_to_batch  to batch
'
share|improve this question
1  
Which value does your function in the VBScript return, and how? Please show us some code. –  Ansgar Wiechers Jul 26 '13 at 11:37
    
This might help groups.google.com/forum/#!topic/… –  Geoff Jul 26 '13 at 11:38

2 Answers 2

up vote 3 down vote accepted

UPDATED

Ok, so you need to return a non-integer. The code below will work for non-integers as well as text.

This is an example, you will need to modify it to fit your needs.

In your VB Script, do this:

WScript.Echo 99.99999

In your batch file, do this:

FOR /F "usebackq tokens=*" %%r in (`CSCRIPT "MyVBS.vbs"`) DO SET RESULT=%%r
ECHO %RESULT%
share|improve this answer
    
It's not an integer unfortunately. It is a real number. –  user2622509 Jul 26 '13 at 12:19
    
It doesn't work :(. It pops up a message box with the result and in the command line it says ECHO is ON –  user2622509 Jul 26 '13 at 12:39
    
Can you show us the full (or least relevant) portion of your actual VB Script? That message indicates that the VB Script is not writing anything to the screen, so RESULT does not get set to anything. Then, the ECHO command runs with no parameter, which gives you that message. –  aphoria Jul 26 '13 at 12:41
    
Now it is working!!! Thank you –  user2622509 Jul 26 '13 at 12:51
    
If this answer helped you, an upvote and accepting the answer would be very nice of you. :) –  aphoria Jul 26 '13 at 12:55

How about using an environment variable as an interface between your batch file and vbscript file parameter passing?

share|improve this answer
    
Could you please show me a very simple example? –  user2622509 Jul 26 '13 at 12:14
    
That doesn't work, because it's 2 separate processes, and one process cannot modify the environment of another process. –  Ansgar Wiechers Jul 27 '13 at 11:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.