Return value from a CScript call?

17 views
Skip to first unread message

John Stockton

unread,
Nov 11, 2022, 6:35:58 PM11/11/22
to
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

unread,
Nov 12, 2022, 1:47:17 AM11/12/22
to
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

unread,
Nov 20, 2022, 7:01:20 AM11/20/22
to
John,

> 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


Reply all
Reply to author
Forward
0 new messages