Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Return value from VBScript to cmd file

Reply
 
Old 11-13-2008   #1 (permalink)
BrianB


 
 

Return value from VBScript to cmd file

I'm modifying a Windows command script that needs to use a value from the
Win32_OperatingSystem class. How can I call a VBScript from inside the cmd
script and return a value to the cmd script?

Brian Bygland



My System SpecsSystem Spec
Old 11-13-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: Return value from VBScript to cmd file


"BrianB" <brian@xxxxxx> wrote in message
news:%23w0kMEVRJHA.4916@xxxxxx
Quote:

> I'm modifying a Windows command script that needs to use a value from the
> Win32_OperatingSystem class. How can I call a VBScript from inside the cmd
> script and return a value to the cmd script?
>
> Brian Bygland
Your command script (=batch file) could look like so
@echo off
{Your code}
cscript //nologo c:\Scripts\YourScript.vbs
echo Return code = %ErrorLevel%
{More of your own code}

Your VB Script could terminate like so:
wscript.quite 9


My System SpecsSystem Spec
Old 11-13-2008   #3 (permalink)
Tom Lavedas


 
 

Re: Return value from VBScript to cmd file

On Nov 13, 1:58*am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> "BrianB" <br...@xxxxxx> wrote in message
>
> news:%23w0kMEVRJHA.4916@xxxxxx
>
Quote:

> > I'm modifying a Windows command script that needs to use a value from the
> > Win32_OperatingSystem class. How can I call a VBScript from inside the cmd
> > script and return a value to the cmd script?
>
Quote:

> > Brian Bygland
>
> Your command script (=batch file) could look like so
> @echo off
> {Your code}
> cscript //nologo c:\Scripts\YourScript.vbs
> echo Return code = %ErrorLevel%
> {More of your own code}
>
> Your VB Script could terminate like so:
> wscript.quite 9
Typo in the last line (one that I'm prone to also). It should
read ...

wscript.quit 9

While I'm here let me note that this approach can use a variable, as
well, but that it can only return an integer between 0 and 255 to a
batch procedure. For example, ...

nNumber = 2 + 2
wscript.quit nNumber

If the result is/can be larger than 255, is real or a string then try
something like this instead ...

in script:

Result = 123.456 ' for exaple
wscript.echo Result

then in batch ...

@echo off
:: {Your code}
for /f "delims=" %%a in ('cscript //nologo c:\Scripts
\YourScript.vbs') do (
set ScriptOut=%%a)
echo Script Result = %ScriptOut%

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Old 11-13-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: Return value from VBScript to cmd file


"Tom Lavedas" <tglbatch@xxxxxx> wrote in message
news:dced8ce7-e6ed-42bb-8758-945639141cac@xxxxxx
On Nov 13, 1:58 am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

> "BrianB" <br...@xxxxxx> wrote in message
>
> news:%23w0kMEVRJHA.4916@xxxxxx
>
Quote:

> > I'm modifying a Windows command script that needs to use a value from
> > the
> > Win32_OperatingSystem class. How can I call a VBScript from inside the
> > cmd
> > script and return a value to the cmd script?
>
Quote:

> > Brian Bygland
>
> Your command script (=batch file) could look like so
> @echo off
> {Your code}
> cscript //nologo c:\Scripts\YourScript.vbs
> echo Return code = %ErrorLevel%
> {More of your own code}
>
> Your VB Script could terminate like so:
> wscript.quite 9
Typo in the last line (one that I'm prone to also). It should
read ...

wscript.quit 9

============

"Quite" so!


My System SpecsSystem Spec
Old 11-13-2008   #5 (permalink)
BrianB


 
 

Re: Return value from VBScript to cmd file

Thank you all. Your suggestions will save my day

Brian

"Pegasus (MVP)" <I.can@xxxxxx> wrote in message
news:OH$tYtZRJHA.3516@xxxxxx
Quote:

>
> "Tom Lavedas" <tglbatch@xxxxxx> wrote in message
> news:dced8ce7-e6ed-42bb-8758-945639141cac@xxxxxx
> On Nov 13, 1:58 am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

>> "BrianB" <br...@xxxxxx> wrote in message
>>
>> news:%23w0kMEVRJHA.4916@xxxxxx
>>
Quote:

>> > I'm modifying a Windows command script that needs to use a value from
>> > the
>> > Win32_OperatingSystem class. How can I call a VBScript from inside the
>> > cmd
>> > script and return a value to the cmd script?
>>
Quote:

>> > Brian Bygland
>>
>> Your command script (=batch file) could look like so
>> @echo off
>> {Your code}
>> cscript //nologo c:\Scripts\YourScript.vbs
>> echo Return code = %ErrorLevel%
>> {More of your own code}
>>
>> Your VB Script could terminate like so:
>> wscript.quite 9
>
> Typo in the last line (one that I'm prone to also). It should
> read ...
>
> wscript.quit 9
>
> ============
>
> "Quite" so!
>

My System SpecsSystem Spec
Old 11-13-2008   #6 (permalink)
Jim de Graff


 
 

Re: Return value from VBScript to cmd file

Have you considered setting an environment variable in your script to the
value (string) that you want returned?

"BrianB" <brian@xxxxxx> wrote in message
news:uam%23wWbRJHA.3628@xxxxxx
Quote:

> Thank you all. Your suggestions will save my day
>
> Brian
>
> "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
> news:OH$tYtZRJHA.3516@xxxxxx
Quote:

>>
>> "Tom Lavedas" <tglbatch@xxxxxx> wrote in message
>> news:dced8ce7-e6ed-42bb-8758-945639141cac@xxxxxx
>> On Nov 13, 1:58 am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

>>> "BrianB" <br...@xxxxxx> wrote in message
>>>
>>> news:%23w0kMEVRJHA.4916@xxxxxx
>>>
>>> > I'm modifying a Windows command script that needs to use a value from
>>> > the
>>> > Win32_OperatingSystem class. How can I call a VBScript from inside the
>>> > cmd
>>> > script and return a value to the cmd script?
>>>
>>> > Brian Bygland
>>>
>>> Your command script (=batch file) could look like so
>>> @echo off
>>> {Your code}
>>> cscript //nologo c:\Scripts\YourScript.vbs
>>> echo Return code = %ErrorLevel%
>>> {More of your own code}
>>>
>>> Your VB Script could terminate like so:
>>> wscript.quite 9
>>
>> Typo in the last line (one that I'm prone to also). It should
>> read ...
>>
>> wscript.quit 9
>>
>> ============
>>
>> "Quite" so!
>>
>
>

My System SpecsSystem Spec
Old 11-13-2008   #7 (permalink)
Pegasus \(MVP\)


 
 

Re: Return value from VBScript to cmd file


"Jim de Graff" <rjdegraff@xxxxxx> wrote in message
news:eIDF7lbRJHA.1448@xxxxxx
Quote:

> Have you considered setting an environment variable in your script to the
> value (string) that you want returned?
How exactly would you set an environmental variable in a script so that it
is available to the calling program?


My System SpecsSystem Spec
Old 11-13-2008   #8 (permalink)
BrianB


 
 

Re: Return value from VBScript to cmd file

"Pegasus (MVP)" <I.can@xxxxxx> wrote in message
news:eJn2ZsbRJHA.4608@xxxxxx
Quote:

>
> "Jim de Graff" <rjdegraff@xxxxxx> wrote in message
> news:eIDF7lbRJHA.1448@xxxxxx
Quote:

>> Have you considered setting an environment variable in your script to the
>> value (string) that you want returned?
>
> How exactly would you set an environmental variable in a script so that it
> is available to the calling program?
That would be a much more elegant solution if it is possible.

Brian


My System SpecsSystem Spec
Old 11-13-2008   #9 (permalink)
BrianB


 
 

Re: Return value from VBScript to cmd file

>"Tom Lavedas" <tglbatch@xxxxxx> wrote in message
Quote:

>news:dced8ce7-e6ed-42bb-8758->945639141cac@xxxxxx
>On Nov 13, 1:58 am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote:
Quote:

>> "BrianB" <br...@xxxxxx> wrote in message
>>
>> news:%23w0kMEVRJHA.4916@xxxxxx
>>
Quote:

>> > I'm modifying a Windows command script that needs to use a value from
>> > the
>> > Win32_OperatingSystem class. How can I call a VBScript from inside the
>> > cmd
>> > script and return a value to the cmd script?
>>
Quote:

>> > Brian Bygland
<snip>
Quote:

>If the result is/can be larger than 255, is real or a string then try
>something like this instead ...
>
>in script:
>
> Result = 123.456 ' for exaple
> wscript.echo Result
>
>then in batch ...
>
> @echo off
>:: {Your code}
> for /f "delims=" %%a in ('cscript //nologo c:\Scripts
>\YourScript.vbs') do (
> set ScriptOut=%%a)
> echo Script Result = %ScriptOut%
>
>Tom Lavedas
Tom, One question. If the script returns multiple lines of output, your
example only catches the last line. How do I get it to capture the entire
output. I tried:

Set ScriptOut=
for /f "delims=" %%Z in ('cscript //nologo c:\cc_trigs\jvbsout.vbs') do (
Set BVBTemp=%%Z
Set ScriptOut=%ScriptOut%%BVBTemp%
)
echo Script output: %BVBTemp%

But this still just saves the last line.

Brian


My System SpecsSystem Spec
Old 11-13-2008   #10 (permalink)
Tom Lavedas


 
 

Re: Return value from VBScript to cmd file

On Nov 13, 1:42*pm, "BrianB" <br...@xxxxxx> wrote:
Quote:

> "Pegasus (MVP)" <I....@xxxxxx> wrote in message
>
> news:eJn2ZsbRJHA.4608@xxxxxx
>
Quote:

> > "Jim de Graff" <rjdegr...@xxxxxx> wrote in message
> >news:eIDF7lbRJHA.1448@xxxxxx
Quote:

> >> Have you considered setting an environment variable in your script to the
> >> value (string) that you want returned?
>
Quote:

> > How exactly would you set an environmental variable in a script so thatit
> > is available to the calling program?
>
> That would be a much more elegant solution if it is possible.
>
> Brian
Unfortunately, it's not possible - at least achieving it would ruin
its 'elegance' because of the 'hacks' needed to make it happen.

The reason it's not possible (within reason) is that a 'child' process
gets a *temporary COPY* of the environment. A script can modify this
COPY, but since it is temporary, it is NOT available to the calling
process once the script ends. Access to the non-volatile version of
the environment requires access to the Windows API, which cannot be
done easily in script. A supporting application could be called, but
all in all it seems much messier to me than the use of the FOR
statement.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Return Value From VBScript In .NET .NET General
Return Powershell Error to VBscript PowerShell
File Deletion - Gone but Return! Vista General
return only certain lines from large text file PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46