Discussion:
Return value from a CScript call?
(too old to reply)
John Stockton
2022-11-11 23:35:57 UTC
Permalink
I have a batch file containing this line :-
CScript //nologo THING.JS

That *.JS file finishes with :-
WScript.echo(S)

so that I can read the answer S in a small window.

How, most straightforwardly, can I get the value of S (an alphanumeric string of 10 characters) back to the batch file, for example as a local environment variable accessible as %FRED% ??

I have thought of using something like (untested)
CScript.echo("set FRED=" + S),
redirecting that output to a file ALEC.BAT, and executing that; but there should be a better way.

Speed is of no importance; it will typically be executed once a day at login time. I do NOT want to rewrite THING.JS in pure Batch code.
--
(c) John Stockton, near London, UK. Using Google Groups. |
JJ
2022-11-12 06:46:53 UTC
Permalink
Post by John Stockton
I have a batch file containing this line :-
CScript //nologo THING.JS
That *.JS file finishes with :-
WScript.echo(S)
so that I can read the answer S in a small window.
How, most straightforwardly, can I get the value of S (an alphanumeric string of 10 characters) back to the batch file, for example as a local environment variable accessible as %FRED% ??
I have thought of using something like (untested)
CScript.echo("set FRED=" + S),
redirecting that output to a file ALEC.BAT, and executing that; but there should be a better way.
Speed is of no importance; it will typically be executed once a day at login time. I do NOT want to rewrite THING.JS in pure Batch code.
You can redirect the output of CSCRIPT using `for /f` with the CSCRIPT
command line and parse its output. Or the output can be redirected into a
file first if a result file is needed.

e.g. the JS file part (test.js):

WScript.echo("Result from cscript");

The batch file part, with direct parsing, e.g.

@echo off
setlocal
set res=
for /f "delims=" %%A in ('cscript.exe //nologo test.js') do set "res=%%A"
echo result=%res%
R.Wieser
2022-11-20 12:01:11 UTC
Permalink
John,
Post by John Stockton
How, most straightforwardly, can I get the value of S (an alphanumeric
string of
10 characters) back to the batch file, for example as a local environment
variable
accessible as %FRED% ??
I think you will find the answer in here :

https://devblogs.microsoft.com/oldnewthing/20120731-00/?p=7003

Regards,
Rudy Wieser

Loading...