Scripting command line applications & Command Line Prompting 
Author Message
 Scripting command line applications & Command Line Prompting

Scripting command line applications & Command Line Prompting1: Have you assured that your script runs in CScript.exe? You need this host to retrieve StdIn/StdOut. There is also a known bug, which prevents piping from within a script. The better solution was proposed by Michael Harris in several scripts: Redirect the output of a shelled command into a file, and then use the filesystemobject to read this file. Till now I haven't had time to post some samples at the WSH Bazaar, but see the postings within this newsgroup - I guess there are still postings from Michael Harris which indicates it.

2: There is no possibility to retrieve keystrokes in WSH without an input box (or form). I wonder how Perl will do this. In Windows all keystrokes are send to the message queue of the window, which have the focus! If a WSH script doesn't have a focus, no keystrokes can be detedted. I just begun to think about an ActiveX control that provides such a feature - but I failed till now on the problem that makes no sense to "catch" a keystroke, if the script doesn't have the focus.

Ups, haven't recognized that you told about the command line. OK, if you have opened a script in a command prompt window (using CScript.exe), you can use Stdin and Stdout or the FSO object to retrieve user input.

Here is a sample from my WSH tutorial. The ideas was derived from a postings maybe a year ago here in this newsgroup.

'************************************************
' File:   Console.vbs (WSH sample in VBScript)
' Author: G. Born
'
' Demonstrates how the console input may be used.
' Warning: The script must be executed under MS-DOS
' using CScript.exe console.vbs. Otherwise WSH will
' crash!!!
'************************************************
Option Explicit
Dim Test

' test, whether the host is CScript.exe
If (Not IsCScript()) Then
 WScript.Echo "Sorry, only CScript.exe may be used"
 WScript.Quit()          ' terminate script
End If

' We need no additional methods, just inform the user what we intend to do
WScript.Echo "Console input test"

' now try to read something from the console
Test = ConsoleInput("Enter text here: ")

' success: show the result
WScript.Echo "Console input test", vbCRLF, _
  "your input was:", chr(34) & Test & chr(34)

WScript.Quit() ' ready

'###############################
' Helper to read from console
'###############################
Function ConsoleInput(Prompt)
' uses the FileSystemObject to read from MS-DOS
 Const ForReading = 1, ForWriting = 2  ' file access mode
 Dim fso, Con              ' object variable

' create FileSystemObject object
 Set fso = CreateObject("Scripting.FileSystemObject")

' Now we are ready to read or write to the console
' We can use the following command to create an object
' to access the MS-DOS CON: for write access
' Set Con = fso.OpenTextFile("CON", ForWriting)

' Then we can use the following command to write
' Con.Write Prompt   ' display prompt text

' But it's to complicated, instead we use:
 WScript.Echo Prompt

' now open the CON for read access
 Set Con = fso.OpenTextFile("CON", ForReading)

' read the user input
 ConsoleInput =  Con.ReadLine

' cleanup
 Con.Close           ' close "file"
 Set Con = Nothing   ' release object
End Function

Function IsCScript()
' check whether CScript.exe is the host
 If (Instr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then
  IsCScript = true
 Else
  IsCScript = false
 End if
End function
' End

The sample works for both WSH 1.0 and WSH 2.0. Hope this helps.

G. Born

Check out the WSH Bazaar at:

http://www.*-*-*.com/

  All,

  Question 1

  I've read that it is possible to, within a script, call a command line executable, such as tracert.exe, passing it arguments, and retrieiving out put.  I've attempted this using RUN as well as the shell object's stdin and stdout.  

  Any recommendations on docs and or source?

  Question 2

  Is there a way in vbscript, as in Perl prompt, to present a question to the user and return keystrokes at the command line, sans input box?

  Any recommendations on docs and/or source would be appreciated.

  Thanks,

  P



Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Passing values into and out of Command Line application *and* command line prompt in vbscript

2. Microsoft say there are many command line prompts?

3. How to execute a command line command from a *.HTA

4. Sleep command and running a command line in VBScript

5. use the result of the VBS for other application under the command line

6. FAQ & Command line arguments

7. NT Command Line Scripting???

8. Running vb-script from the command line

9. Script running a passed command line.

10. COMMAND LINE SCRIPT

11. Command line Script

12. create a file type for a virtual directory in IIS using a command line script

 

 
Powered by phpBB® Forum Software