How can I call a console app from WSH in same console? 
Author Message
 How can I call a console app from WSH in same console?

I need to run a console program from JScript running under cscript.exe
and I need the output to go DIRECTLY to the same console from which the
script was run.  Is there a way to force the console app to use the
current console?  WScript.Shell.Run always seems to launch a second
console window, which is not acceptable in this scenario.

Any help or ideas are appreciated.  Thanks!‰

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Fri, 01 Feb 2002 03:00:00 GMT  
 How can I call a console app from WSH in same console?

Quote:

> I need to run a console program from JScript running under cscript.exe
> and I need the output to go DIRECTLY to the same console from which the
> script was run.  Is there a way to force the console app to use the
> current console?  WScript.Shell.Run always seems to launch a second
> console window, which is not acceptable in this scenario.

> Any help or ideas are appreciated.  Thanks!‰

AFAIK, it is inherently impossible to run two applications at the same
time in a 'DOS' session, though task swapping is possible - such as with
TSR's.  I seriously doubt that a JScript can be made to task switch or
'terminate and stay resident'.  Therefore, the first application (your
JScript) must terminate before the second application is started for the
process to remain in a single console session.  

It would, therefore, appear to me that a batch file is required to
support sequential operation of two or more applications.  However, I
imagine your intention is to use a single script to launch an
application and then process this information.  There are a couple of
ways I can think of to do this.  First, write your script with two entry
points; one for the initial entry and one for the entry following the
execution of the application - maybe using a command line argument to
control branching within the script.  Then execute the script via a
batch procedure (in a shortcut) something like this ...

:: AppWrapper.bat
  cscript yourscrpt.js First
  your_app.exe
  cscript yourscrpt.js Second

The text output sent to the console can also be redirected from your
script into the application and/or out of your application into your
script using the StdOut and StdIn objects in WSH 2.0 Beta 2, something
like this ...

:: AppWrapper.bat
  cscript yourscrpt.js First | your_app.exe
  cscript yourscrpt.js Second

:: AppWrapper.bat
  cscript yourscrpt.js First
  your_app.exe | cscript yourscrpt.js Second

The other approach I can think of to launch your console application and
collect its output works with WSH version 1.0 and 2.0, though it
actually opens a second console session.  I'm not too familiar with
JScript, so I'll have to use the VBScript syntax to illustrate this.

' Preamble code
' ...
 Set oWSH = WScript.CreateObject("WScript.Shell")
 Set oFS  = WScript.CreateObject("Scripting.FileSystemObject")
 oWSH.Run "%comspec% /c your_app.exe params > ~temp.txt", 0, True
 Set oTemp = oFS.OpenTextFile("~temp.txt", 1)
 sText = oTemp.ReadAll
 oTemp.Close
 oFS.DeleteFile "~temp.txt", True
' Post-process
' ...

This approach won't work if the conole app needs to interact with the
user at run time, though a text file can be substituted for the input
text stream, if the app accepts redirection.  The input text streem
could be collected by your script and written to a temporary file and
redirected into the application in the .Run.  That is ...

 oWSH.Run "%comspec%/c your_app.exe < ~tempI.txt" > ~tempO.txt", _
           0, True

If this does not address your problem, you will need to be more specific
about exactly what you need to accomplish.

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/



Sat, 02 Feb 2002 03:00:00 GMT  
 How can I call a console app from WSH in same console?
Hi Tom,

Quote:
> AFAIK, it is inherently impossible to run two applications at the
> same time in a 'DOS' session, though task swapping is possible - such
> as with TSR's.  I seriously doubt that a JScript can be made to task
> switch or 'terminate and stay resident'.  Therefore, the first
> application (your JScript) must terminate before the second
> application is started for the process to remain in a single console
> session.

Thanks for the response!

There is a way: use the CreateProcess API and not specify the
CREATE_NEW_CONSOLE flag.  I suspect that I will need to write my own
ActiveX control that exposes CreateProcess. :(

What I want is the effect that a BAT file has when it executes other
command line (ie, console) apps.  That is, the output streams to the
same console window.  I can then scroll back through the history in the
Command Prompt.  I can also tell what the script is doing at all times.
Our process takes ~30 minutes to run (and it is only getting longer sad
to say), so it is very important to be able to see what is going on.
This requirement is what eliminates the redirection approach to capture
output from the secondary consoles.

Again, thanks for the input.  If anyone knows how to achieve the
chaining of proces output as with CMD.EXE and BAT files, please jump
right in and save me the trouble of writing/debugging/maintaining a
helper ActiveX control... :)

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sat, 02 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. running console app in same console

2. How to Control a Console (old DOS) App...

3. VBScript front-end to a console app.

4. Execute console app to same window

5. How to Control a Console (old DOS) App...

6. How to Control a Console (old DOS) App...

7. Using WshShell.Run to launch console app

8. WSH output to console

9. HOWTO: Dynamically create, run, and delete a .NET console application from WSH

10. Locking the Console from WSH (VBScript)

11. Interactive console for WSH scripts...

12. console app. vs explorer app.

 

 
Powered by phpBB® Forum Software