From VBS to BAT

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

From VBS to BAT

Post by MigrationUser »

10 Jun 2010 08:54
Teomangia

Hello everyone. In a batch, I need to use a variable modified within a VBS. How can I do? Thanks

----------------------------

#2 10 Jun 2010 10:14
bluesxman


I know VBS can read in system variables, but not sure how that works with writing back. In simple terms, you could do something like this (assuming that the VBS displays the variable contents to console at its final action):

Code: Select all

for /f "usebackq tokens=*" %%a in (`cscript //nologo yourScript.vbs`) do set "yourVariable=%%a"
But I'd hope there's a better way.

cmd | *sh | ruby | chef

----------------------------

#3 10 Jun 2010 10:38

Teomangia


Thanks, but maybe I have not explained well.

prova.vbs
a = 10
b = 5
c = a + b
d= c + a

prova.bat
cscript prova.vbs
echo %c%
echo%d%

I need to get %c% and %d%.

Thanks

----------------------------

#4 10 Jun 2010 11:30
bluesxman


If you have the VBS display to console like so:

c=<value of c>
d=<value of d>

And then use this variation on the line I posted before:

Code: Select all

for /f "usebackq tokens=1* delims==" %%a in (`cscript //nologo yourScript.vbs`) do set "%%a=%%b"
cmd | *sh | ruby | chef

----------------------------

#5 10 Jun 2010 18:48
Simon Sheppard


You can read and write Environment Variables with both CMD and VBS
But theres a big catch, the CMD shell reads in all environment variables when you startup CMD.exe so that approach won't work.

A better method is to either call a VBScript from your batch file passing the values as parameters
https://ss64.com/vb/cscript.html
https://ss64.com/vb/arguments.html

or the other way around - call a batch file (with CMD/c) from VBScript passing the values as parameters.
https://ss64.com/nt/syntax-args.html

----------------------------

#6 11 Jun 2010 09:11
Teomangia


Thanks for your help.
I could not find what I needed. sad
But I was making a big mistake.
I wanted to use a batch file as a main program because it is executable.
But I can only run the BAT file to invoke the main program VBS.
The main program will perform various VBS VBScript with different parameters.
Now I understand how to invoke another VBS from a VBS
Correct me if I'm wrong
Thanks.

Last edited by Teomangia (11 Jun 2010 09:25)

----------------------------

#7 11 Jun 2010 16:10
Simon Sheppard


To call one VBScript from another see the example at the bottom of this page:

https://ss64.com/vb/run.html

----------------------------

#8 12 Jun 2010 09:17
Teomangia
Simon Sheppard wrote:

To call one VBScript from another see the example at the bottom of this page:

https://ss64.com/vb/run.html
Thanks Simon.

I saw the example.
But how can I get the variables from the batch that I run?

WScript.Quit returns only one value

----------------------------

#9 16 Jun 2010 04:20
sarbjitsinghgill


The best way is as bluesxman said.

Or drop a file in vbs and pickup in .bat.

Other than that,

I am not sure if there is a way to do that; here is why. As Simon said, your bat file already have an ENV block inherited from system. When you will spawn vbs from it, it can inherit the block from bat env. But being a child it cannot modify its parent's env, if you do it will not actually reflect the result back a level up.

There could be a method when you do system programming at OS level, not prefered being dirty way, it can destabilize the system.

So again bluesxman way is the best and easy way.

Still having problem, attach both of your scripts, you will get it done fast.

Regards

Last edited by sarbjitsinghgill (16 Jun 2010 04:20)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer

----------------------------

#10 16 Jun 2010 04:27
sarbjitsinghgill


Also, vbs to vbs will be same situation.

So in berief,
As I do in such a situation, pass to child - parameters. passback to parent- output as string and process the string in parent environment.

e.g.
Dropping a file is even easier in bat files ( echo.%date% >date.today.txt )
Reading it in parent. ( for /f %%x in (date.today.txt) do set mydate=%%X)

HTH

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer
Post Reply