Solved

Write to console in VBScript

Posted on 2003-11-29
7
55,122 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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