Re: Returning a value from a VBScript



On Sep 13, 11:06 am, Matthias Tacke <Matth...@xxxxxxxx> wrote:
Pegasus (MVP) wrote:
"thecomputerguy74" <rgbl...@xxxxxxxxx> wrote in message
news:1189682751.346860.293120@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have searched the web for this, and maybe I'm using the wrong search
terms. I am trying to figure out how to return a value from my
VBScript for use by another script, batch file, or other command. All
I ever seem to find is how to return a value from a function, which
does me now good.

Any help would be appreciated. I'm sure it's not difficult, but I
don't know how to do it.

Thank you.

thecomputerguy74

Try this extremely simplified example:
@echo off
MyScript.vbs
echo MyScript.vbs returned an ErrorLevel of %ErrorLevel%

MyScript.vbs could contain this line:
WScript.Quit(2007)

If you need more digits as errorlevel allows (AFAIK 16 bit signed integer)
you can parse standard output of cscript in a batch:

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
echo Yesterday=%yesterday%
DEL tmp.vbs

This task is much more complicated in pure batch (albeit possible)

--
Greetings
Matthias

In addition to the use of the exit code (Errorlevel) and the FOR
statement in Batch, I can think of at least four other approaches:

1). Write the data from the 'called' script into a temporary file to
be read by the 'calling' script - possibly using the exit code as a
suffix to a predetermined file name to keep old data from being
accessed.

2). Use the WScript.SHELL Exec method to run the 'called' script and
then collect its output stream. This is a variation on the approach
in 1 above that does not require a file, but its more complicated.

2). Use the system's clipboard. This approach is somewhat complicated
by the advent of IE 7, which added a security setting that needs to be
changed for this to work. Two enabling subroutines for this are ...

' Requires IE v5.0+
sub putClipBoardText5(sText)
With CreateObject("InternetExplorer.Application")
.Navigate "about:blank"
Do Until .ReadyState = 4 : WScript.Sleep 50 : Loop
.document.ParentWindow.ClipboardData.SetData "text", sText
end with ' IE
End Sub

' Requires IE v5.0+
Function getClipBoardText5()
With CreateObject("htmlfile")
On Error Resume Next
getClipBoardText5 = .ParentWindow.ClipboardData.GetData("text")
end with ' htmlfile
End Function

3). Use Michael Harris' IEPipe approach. Once this rather
complicated approach is understood, it permits the passing multiple
values of varying data type, even arrays and objects. Do a
google.groups search for Harris as the author and IEPipe as the key
word for information.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

.



Relevant Pages

  • Re: How to STOP file - a command line & script - where do you put it to work?
    ... That batch is for a very specific problem. ... that in and tell it which folders to change the names in? ... I'm a PC user, not a software, java or script> writer. ... > This is the script that was given: > @echo off ...
    (microsoft.public.windowsxp.general)
  • Re: How to STOP file - what is a command line & script?
    ... That batch is for a very specific problem. ... I'm a PC user, not a software, java or script writer. ... > @echo off ... > type nul> %temp%.\process.bat ...
    (microsoft.public.windowsxp.general)
  • Re: How do I change default permissions for shares?
    ... Doing a batch would mean ... Explorer so the only thing you need to is type in the share name the script ... If your share name is always the folder name or the folder name with ... @echo off ...
    (microsoft.public.windowsxp.security_admin)
  • Re: help needed
    ... :> returns an exit code of 0 to the script! ... AFAIK 'Echo', as most internal dos commands, doesn't change the existing ... You are correct that ECHO doesn't change the errorlevel in the batch file, but I can tell you from repeated testing that it does make a difference in what is returned to the Run method. ...
    (microsoft.public.scripting.vbscript)
  • Re: help needed
    ... returns an exit code of 0 to the script! ... AFAIK 'Echo', as most internal dos commands, doesn't change the existing ... get the ERRORLEVEL value from this bat file into an VBScript. ...
    (microsoft.public.scripting.vbscript)