Page 1 of 1

Getting Results From VBScript

Posted: 14 Oct 2011 12:59
by shadeclan
I'm not exactly sure what I'm doing wrong here. Per an individual who provided a great answer to a related question, I wrote the following batch file:

Code: Select all

@echo off
setlocal
   set msgbox="msgbox.vbs"
   
   @echo:wscript.quit(msgbox(wscript.arguments(0),wscript.arguments(1),wscript.arguments(2))) > %msgbox%

   cscript //nologo %msgbox% "Test Message" 1 "Test Title"

   @echo:%errorlevel%
   pause

endlocal
exit /b


Whenever I execute the batch script, the return code is always zero [0]. So, I ditched the message box and just tried returning any old integer...

Code: Select all

@echo off
setlocal

   @echo:wscript.quit(1) > msgbox.vbs

   cscript msgbox.vbs

   echo:%errorlevel%
   pause

endlocal
rem exit /b

... and that doesn't work either.

I'm thinking that there is something wrong with my environment. Is there some sort of switch that needs to be set in order to make this work?

Re: Getting Results From VBScript

Posted: 14 Oct 2011 13:10
by aGerman
Both scripts are working for me and actually I have no clue why they won't work for you.

Regards
aGerman

Re: Getting Results From VBScript

Posted: 14 Oct 2011 13:29
by shadeclan
aGerman wrote:Both scripts are working for me and actually I have no clue why they won't work for you.

Regards
aGerman
There must be something wrong with my computer.

In an attempt at an alternate route, I decided to try creating a file from which I could read a result:

Code: Select all

Dim objFS
Dim objOut

Set objFS = CreateObject("Scripting.fileSystemObject")
Set objOut = objFS.CreateTextFile("Result.txt", TRUE)

objOut.WriteLine("s")
objOut.Close

set objFS = Nothing

WScript.Quit(0)
After a little wrangling, I found out that my fileSystemObject (scrrun.dll) was not registered! I got this code to work after I registered the thing.

I'm thinking that, maybe there is a problem with some other dll connected with VBScript that is not propagating the return value to the errorlevel variable. Anybody have any idea what object that might be?

Re: Getting Results From VBScript

Posted: 17 Oct 2011 12:29
by shadeclan
Well, the following code (in a round-about manner) provides me with what I need in spite of the WScript.Quit function not providing me a proper return code for whatever reason:

Code: Select all

@echo off
setlocal
   set "vMsgBx=msgbox.vbs"
   set "vRsltTxt=Results.txt"
   set "vRslt=vRslt"

   @echo:'Message Box VBScript > %vMsgBx%
   @echo:Dim oFS >> %vMsgBx%
   @echo:Dim oTxtFile >> %vMsgBx%
   @echo:Dim vRslt >> %vMsgBx%
   @echo. >> %vMsgBx%
   @echo:Set oFS = CreateObject("Scripting.FileSystemObject") >> %vMsgBx%
   @echo:Set oTxtFile = oFS.CreateTextFile("%vRsltTxt%", True) >> %vMsgBx%
   @echo. >> %vMsgBx%
   @echo:vRslt = msgbox(wscript.arguments(0),wscript.arguments(1),wscript.arguments(2)) >> %vMsgBx%
   @echo. >> %vMsgBx%
   @echo:oTxtFile.WriteLine(vRslt) >> %vMsgBx%
   @echo:oTxtFile.Close >> %vMsgBx%
   @echo. >> %vMsgBx%
   @echo:WScript.Quit(vRslt) >> %vMsgBx%

   cscript %vMsgBx% //nologo "Some message" 1 "Some title"
   set /p vRslt=<%vRsltTxt%

   del %vMsgBx%
   del %vRsltTxt%

   @echo:Message Box Result = %vRslt%
   pause

endlocal
exit /b 0

Wish I knew why the wscript.quit function wasn't working. :?

Re: Getting Results From VBScript

Posted: 17 Oct 2011 13:30
by nitt

Code: Select all

@echo off
set body=Do you wish to open Notepad.exe?
set title=Alert
set type=vbyesno

echo x=msgbox("%body%",%type%,"%title%") : wscript.stdout.writeline(x) > ~tmp.vbs
for /f "tokens=*" %%a in ('cscript //nologo ~tmp.vbs') do (
set results=%%a
)

del ~tmp.vbs

if "%results%"=="6" start notepad


You want to add the line "wscript.stdout.writeline" at the end of your VBScript, and then you can put the output there and use "for /f" to retrieve it. Here, if you press "yes" then it will open notepad. "6" is the value of the "yes" (vbyes) button in VBScript.