Return value from VBScript to cmd file

B

BrianB

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 Computer

P

Pegasus \(MVP\)

"BrianB" <[email protected]> wrote in message
news:%[email protected]

> 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 Computer

T

Tom Lavedas

On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

> "BrianB" <[email protected]> wrote in message
>
> news:%[email protected]
>

> > 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

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 Computer

P

Pegasus \(MVP\)

"Tom Lavedas" <[email protected]> wrote in message
news:[email protected]
On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

> "BrianB" <[email protected]> wrote in message
>
> news:%[email protected]
>

> > 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 Computer

B

BrianB

Thank you all. Your suggestions will save my day

Brian

"Pegasus (MVP)" <[email protected]> wrote in message
news:[email protected]

>
> "Tom Lavedas" <[email protected]> wrote in message
> news:[email protected]
> On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

>> "BrianB" <[email protected]> wrote in message
>>
>> news:%[email protected]
>>

>> > 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 Computer

J

Jim de Graff

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

"BrianB" <[email protected]> wrote in message
news:uam%[email protected]

> Thank you all. Your suggestions will save my day
>
> Brian
>
> "Pegasus (MVP)" <[email protected]> wrote in message
> news:[email protected]

>>
>> "Tom Lavedas" <[email protected]> wrote in message
>> news:[email protected]
>> On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

>>> "BrianB" <[email protected]> wrote in message
>>>
>>> news:%[email protected]
>>>
>>> > 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 Computer

B

BrianB

"Pegasus (MVP)" <[email protected]> wrote in message
news:[email protected]

>
> "Jim de Graff" <[email protected]> wrote in message
> news:[email protected]

>> 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 Computer

B

BrianB

>"Tom Lavedas" <[email protected]> wrote in message

>news:dced8ce7-e6ed-42bb-8758->[email protected]
>On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

>> "BrianB" <[email protected]> wrote in message
>>
>> news:%[email protected]
>>

>> > 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
<snip>

>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 Computer

T

Tom Lavedas

On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:

> "Pegasus (MVP)" <[email protected]> wrote in message
>
> news:[email protected]
>

> > "Jim de Graff" <[email protected]> wrote in message
> >news:[email protected]

> >> 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 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 Computer

T

Tom Lavedas

On Nov 13, 1:59 pm, "BrianB" <[email protected]> wrote:

> >"Tom Lavedas" <[email protected]> wrote in message
> >news:dced8ce7-e6ed-42bb-8758->945639141__BEGIN_MASK_n#[email protected]
> >On Nov 13, 1:58 am, "Pegasus \(MVP\)" <[email protected]> wrote:

> >> "BrianB" <[email protected]> wrote in message
>
>

> >> > 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
> <snip>

> >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
Yes, that's a little trickier. It required delayed variable expansion
to be enabled. If it is just a few lines (one to three or so), then
I'd use something like this (not too far from what you already
have) ...

Set ScriptOut1=
Set ScriptOut2=
Set ScriptOut3=
setlocal enabledelayedexpansion
for /f "delims=" %%Z in ('cscript //nologo c:\cc_trigs\jvbsout.vbs')
do (
Set ScriptOut1=!ScriptOut2!
Set ScriptOut2=!ScriptOut3!
Set ScriptOut3=%%Z
)
echo Script output: %ScriptOut1%, %ScriptOut2%, %ScriptOut3%

Or it can also be done in a subroutine ...

Set ScriptOut1=
Set ScriptOut2=
Set ScriptOut3=
for /f "delims=" %%Z in ('cscript //nologo c:\cc_trigs\jvbsout.vbs')
do (
call :Sub "%%Z")
echo Script output: %ScriptOut1%, %ScriptOut2%, %ScriptOut3%
goto :EOF

:Sub
Set ScriptOut1=%ScriptOut2%
Set ScriptOut2=%ScriptOut3%
Set ScriptOut3=%~1
:: End of Sub

This second approach does not require the Delayed Expansion, so that
the contents of the variables persist following the routine's
completion - if that's a requirement.

All of these assume that all three lines are ALWAYS created. The
output is collected in all cases, but which variable contains which
output may be an issue.

This can be avoided if the output is of the form ...

wsh.echo "Output1=" & Result1
wsh.echo "Output2=" & Result2
wsh.echo "Output3=" & Result3
wsh.echo "Output4=" & Result4

Then the batch procedure becomes much simpler ...

for /f "delims=" %%Z in ('cscript //nologo c:\cc_trigs\jvbsout.vbs')
do set %%z

This works for any number of unique variable names (string before the
equal sign).

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
 

My Computer

P

Pegasus \(MVP\)

"Tom Lavedas" <[email protected]> wrote in message
news:[email protected]
On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:

> "Pegasus (MVP)" <[email protected]> wrote in message
>
> news:[email protected]
>

> > "Jim de Graff" <[email protected]> wrote in message
> >news:[email protected]

> >> 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
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

***********

Exactly - that's why I asked.
 

My Computer

B

BrianB

"Tom Lavedas" <[email protected]> wrote in message
news:[email protected]
On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:

> "Pegasus (MVP)" <[email protected]> wrote in message
>
> news:[email protected]
>

> > "Jim de Graff" <[email protected]> wrote in message
> >news:[email protected]

> >> 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
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/

POO. That's what I thought.

Thanks anyway.
Brian
 

My Computer

B

BigDaddyJim

On Nov 14, 9:35 am, "BrianB" <[email protected]> wrote:

> "Tom Lavedas" <[email protected]> wrote in message
>
> news:[email protected]
> On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:
>
>
>
>
>

> > "Pegasus (MVP)" <[email protected]> wrote in message
>
>

> > > "Jim de Graff" <[email protected]> wrote in message
> > >news:[email protected]
> > >> 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
>
> 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/
>
> POO. That's what I thought.
>
> Thanks anyway.
> Brian- Hide quoted text -
>
> - Show quoted text -
Use the SENDKEYS method to type your text into the CMD prompt.
 

My Computer

T

Tom Lavedas

On Nov 14, 6:00 pm, BigDaddyJim <[email protected]> wrote:

> On Nov 14, 9:35 am, "BrianB" <[email protected]> wrote:
>
>
>
>
>
>
>

> > "Tom Lavedas" <[email protected]> wrote in message
>

> >news:[email protected]
> > On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:
>

> > > "Pegasus (MVP)" <[email protected]> wrote in message
>
>

> > > > "Jim de Graff" <[email protected]> wrote in message
> > > >news:[email protected]
> > > >> Have you considered setting an environment variable in your scriptto
> > > >> 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.
>
>

> > 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/
>

> > POO. That's what I thought.
>

> > Thanks anyway.
> > Brian- Hide quoted text -
>

> > - Show quoted text -
>
> Use the SENDKEYS method to type your text into the CMD prompt.
Care to offer a sample? I doubt if you can - and even if you can, I
still see it as a hack and a kludge that is far messier than the
simple FOR construct.

Tom Lavedas
-------------------
 

My Computer

J

Jim de Graff

I kinda put my foot in it. I was asked a while back to write a standalone
program to display a date picker. The idea was to start specific operator
programs with the given date. These programs were all script based so it was
easier to have an exe that displayed the date picker and store the resulting
date in an environment variable which could then be accessed by the next
script (or set of scripts) to run. I had not considered that the same
approach would not work in a cmd file for the reasons stated so many times
in the ensuing responses.
 

My Computer

T

Tom Lavedas

On Nov 14, 10:09 pm, Tom Lavedas <[email protected]> wrote:

> On Nov 14, 6:00 pm, BigDaddyJim <[email protected]> wrote:
>
>
>

> > On Nov 14, 9:35 am, "BrianB" <[email protected]> wrote:
>

> > > "Tom Lavedas" <[email protected]> wrote in message
>

> > >news:[email protected]
> > > On Nov 13, 1:42 pm, "BrianB" <[email protected]> wrote:
>

> > > > "Pegasus (MVP)" <[email protected]> wrote in message
>
>

> > > > > "Jim de Graff" <[email protected]> wrote in message
> > > > >news:[email protected]
> > > > >> 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.
>
>

> > > 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.
>
>

> > > POO. That's what I thought.
>

> > > Thanks anyway.
> > > Brian- Hide quoted text -
>

> > > - Show quoted text -
>

> > Use the SENDKEYS method to type your text into the CMD prompt.
>
> Care to offer a sample? I doubt if you can - and even if you can, I
> still see it as a hack and a kludge that is far messier than the
> simple FOR construct.
>
> Tom Lavedas
> -------------------
OK, so there is a way - but it *IS* a kludge!

The example belowevaluates the input string (enclosed in quotes) and
stores the result in the e-var,, Res, using SENDKEYS.

:: SETVAR.CMD
@echo off
if '%1==' goto :EOF
set t=%temp%\tmp.vbs
echo Out = eval(%1)> %t%
echo createobject("wscript.shell").sendkeys Out^&"~" >> %t%
wscript %t% & del %t% & set "t="
set /p Res=
echo Result = %Res%

However, the equivalent using the FOR seems more straightforward to
me ...

::FORVAR.CMD
@echo off
if '%1==' goto :EOF
set t=%temp%\tmp.vbs
echo Out = eval(%1)> %t%
echo wsh.echo "res="^&Out >> %t%
for /f "delims=" %%a in ('cscript //nologo %t%') do set %%a
del %t% & set "t="
echo Result = %Res%

Plus, there's no screen clutter as with the SENDKEYS approach.

BTW, try this command line and see what you get ...

forvar "year(now)&month(now)&day(now)"

or ...

setvar "sqr(2)"

This approach is a great little routine for accessing VBScript
functionality in batch if it can be stated in one line of text. I've
found that a surprising array of capabilities can.

Tom Lavedas
-------------------
 

My Computer

Top