Solved

Write to console in VBScript

Posted on 2003-11-29
7
55,491 Views
Last Modified: 2011-08-18
Help!!

I have a simple but extreamly frustrating problem.  I have a .vbs file that I am writing to help me debug a web service.  It has a variety of MsgBox statements to show me things I am interested in.

My problem is, I need to pose a question here, and the data being reflected in these baxes is not copy/paste able.  That is all I want to accomplish is to copy what is in the message box.

I tried to write to the console directly, but to no avail.  

       WScript.StdOut.WriteLine " " & responseDocument.xml

Gives me "The handle is invalid.  Code 80070006.  I am new to vbs.  (Give me perl any day.)

Please help me.  I need something quick and effective.  
0
Comment
Question by:jflanner
7 Comments
 
LVL 7

Assisted Solution

by:wsteegmans
wsteegmans earned 250 total points
ID: 9843890
You could use the FileSystemObject to write all your data to a textfile. I think this will help ...

Example:

    ' Set up Constants
    Const ForWriting = 2 ' Input OutPut mode
    Const Create = True
   
    Dim MyFile
    Dim FSO ' FileSystemObject
    Dim TSO ' TextStreamObject
   
    ' Use MapPath function to get the Physical Path of file
   
    MyFile = Server.MapPath("textfile.txt")
   
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
    Set TSO = FSO.OpenTextFile(MyFile, ForWriting, Create)
   
    TSO.Write "This is first line in this text File" & vbCrLf
    ' Vbcrlf is next line character
    TSO.Write "This is Second line in this text file" & vbCrLf
    TSO.Write "Writen by devasp visitor at " & Now()
    TSO.WriteLine ""
   
    Response.Write " Three lines are writen to textfile.txt <br>"
    Response.Write " Local time at server is " & Now()
   
    ' close TextStreamObject and
    ' destroy local variables to relase memory
    TSO.Close
    Set TSO = Nothing
    Set FSO = Nothing

Regards!
0
 
LVL 9

Expert Comment

by:TooKoolKris
ID: 9849938
You aren't trying to run these scripts that make use of the stdOut methods by using Wscript.exe are you. In order to properly the StdIn, StdOut, and StdErr streams can be accessed while using CScript.exe only. So you must run these scripts from the command line.

See if this does anything for you but make sure you run it from a prompt using Cscript. When you type something in at the console it's going to tell you what you typed and what line you did it on.

Dim StdIn, StdOut
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut

Do While Not StdIn.AtEndOfStream
     str = StdIn.ReadLine
     StdOut.WriteLine "Line " & (StdIn.Line - 1) & ": " & str
Loop
0
 
LVL 9

Accepted Solution

by:
TooKoolKris earned 250 total points
ID: 9849949
Here is another one you can play with. The following code starts a batch file and waits for the user input prompt. After entering the needed data through the StdIn stream, the batch file will be able to complete.

Dim WshShell, oExec, input
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("test.bat")
input = ""

Do While True

     If Not oExec.StdOut.AtEndOfStream Then
          input = input & oExec.StdOut.Read(1)
          If InStr(input, "Press any key") <> 0 Then Exit Do
     End If
     WScript.Sleep 100
Loop

oExec.StdIn.Write VbCrLf

Do While oExec.Status <> 1
     WScript.Sleep 100
Loop
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:jflanner
ID: 9878867
Folks:

I appreciate the feedback.  I have learned, but I am not there yet.

wsteegmans:  When I try your method, I get an error that leads me to believe "Server" is not instanciated.  (Remember, I am new to VBS, it is probably something very stupid.  But I need complete examples.)

TooKoolKris:  calling my script from the Command Line: "cscript ArithmeticMean.vbs", coupled with your suggested changes works.  But, as the script goes on, I fail at:
requestHTTP.open "POST", myWebService, false

This line works if I call the script directly: "ArithmeticMean.vbs"

I need the complete script to work.  If either of you can help me out, I'll release the points.  Also, anyone know a good vbs book.
0
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9880654
Hi jflanner,

I wrote the script as a real ASP-Script (also VBS, but little different) ...

Here is the real one (without any Server Objects at all):

    ' Set up Constants
    Const ForWriting = 2 ' Input OutPut mode
    Const Create = True
   
    Dim MyFile
    Dim FSO ' FileSystemObject
    Dim TSO ' TextStreamObject
   
    ' Use MapPath function to get the Physical Path of file
   
    MyFile = "c:\temp\textfile.txt"
   
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set TSO = FSO.OpenTextFile(MyFile, ForWriting, Create)
   
    TSO.Write "This is first line in this text File" & vbCrLf
    ' Vbcrlf is next line character
    TSO.Write "This is Second line in this text file" & vbCrLf
    TSO.Write "Just an other line ... written " & Now()
    TSO.WriteLine ""
   
    ' close TextStreamObject and
    ' destroy local variables to relase memory
    TSO.Close
    Set TSO = Nothing
    Set FSO = Nothing

Don't know a good VBS Book. Learned VB and VBA, and VBS is just a simple version of them. So, if you know VB or VBA, you also know a lot of VBS.

Regards!
0
 
LVL 9

Expert Comment

by:TooKoolKris
ID: 9881918
Well I don't know what to tell you as both scripts work fine for me. Without being in front of your program it's hard to say why they aren't working for you. I assure you nothing is wrong with the scripts however.

The best book for VBS in IMO is this:

Microsoft Windows 2000 Scripting Guide
http://www.amazon.com/exec/obidos/tg/detail/-/0735618674/qid=1070629091/sr=1-1/ref=sr_1_1/102-5718454-3657735?v=glance&s=books
0
 

Expert Comment

by:briangs
ID: 23150167
Your client is running WScript Engine as the default and you are executing a CScript command. Switch to the CScript Engine with the command "wscript /h:cscript" and your command will work fine.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now