Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get a return value from a vbscript in BAT file

2,596 views
Skip to first unread message

Viatcheslav V. Vassiliev

unread,
Jan 30, 2004, 2:53:51 PM1/30/04
to
You should use %ERRORLEVEL% variable.

//---test.bat
cscript test.vbs
echo %ERRORLEVEL%

//--test.vbs
WScript.Quit 1

//------------------------------------
Regards,
Vassiliev V. V.
http://www-sharp.com - best scripting/HTA IDE

"Tomtom" <anon...@discussions.microsoft.com> сообщил/сообщила в новостях
следующее: news:8DCC61E2-7D17-43E2...@microsoft.com...
> I am calling a vbscript from a batch file. It'd be conveninent for me if I
can get a return value from the vbscript and use the value in the batch
file. Is this possible?


Tom Lavedas

unread,
Jan 30, 2004, 4:32:52 PM1/30/04
to
Another approach that can return any kind of argument is
to simply wsh.Echo it out in the form ...

wsh.echo "set Result=" & MyResult

then execute the script in a FOR with a /F switch, like
this ...

for /f "delims=" %%a in (
'cscript yourscript.vbs') do %%a
echo The script returned: %Result%

This is for Win NT/2K/XP.

Other OS's require redirection into a temporary batch file
that is then CALLed and deleted.

Tom Lavedas
===========

>-----Original Message-----
>You should use %ERRORLEVEL% variable.
>
>//---test.bat
>cscript test.vbs
>echo %ERRORLEVEL%
>
>//--test.vbs
>WScript.Quit 1
>
>//------------------------------------
>Regards,
>Vassiliev V. V.
>http://www-sharp.com - best scripting/HTA IDE
>
>"Tomtom" <anon...@discussions.microsoft.com>

ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ
>ÓÌÅÄÕÀÝÅÅ: news:8DCC61E2-7D17-43E2-8BEB-
1E1063...@microsoft.com...

ravensha

unread,
Jan 30, 2004, 5:24:19 PM1/30/04
to
Have you tried using an Enviroment Variable?
I haven't done anything with 'em , but if i remember correctly, wscript can
access/modify enviroment variables.
Theoretically, you should be able to have a bat file and a wsh script file
"talk" back and forth, as much as you want
using your own enviroment variables.

"Tomtom" <anon...@discussions.microsoft.com> wrote in message
news:8DCC61E2-7D17-43E2...@microsoft.com...

Torgeir Bakken (MVP)

unread,
Jan 30, 2004, 5:37:53 PM1/30/04
to
ravensha wrote:

> Have you tried using an Enviroment Variable?
> I haven't done anything with 'em , but if i remember correctly, wscript can
> access/modify enviroment variables.
> Theoretically, you should be able to have a bat file and a wsh script file
> "talk" back and forth, as much as you want
> using your own enviroment variables.

Hi

Updating the environment of a parent process is not trivial...


--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


ravensha

unread,
Feb 1, 2004, 8:32:32 PM2/1/04
to
Sorry, I meant custom variables such as
set myVar = %PATH%
in a bat file that can in turn be checked by wsh.

"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message
news:401ADCC1...@hydro.com...

Al Dunbar [MS-MVP]

unread,
Feb 1, 2004, 11:41:11 PM2/1/04
to

"ravensha" <anti.spam...@stis.net.anti.spam> wrote in message
news:f099ff6ce35c0c19...@news.teranews.com...

> Sorry, I meant custom variables such as
> set myVar = %PATH%
> in a bat file that can in turn be checked by wsh.

If the batch file issues that set command before running a wsh script, the
wsh script will have no trouble finding the value of that variable. As
Torgeir says, though, actually modifying that variable so a new value will
be seen by the batch file after the wsh script has completed is non-trivial.
And this has nothing to do with whether the variable is a standard OS
variable like PATH, or a custom, script-specific variable like myVar.

/Al

0 new messages