Solved

Need Explanation of Cscript vs. Wscript (batch mode, console mode, interactive mode)

Posted on 2010-08-20
9
2,354 Views
Last Modified: 2012-08-13
These are all so similar.  Most references just seem to describe them sequentially, without really pointing out any differences.

Can someone directly compare DIFFERENCES or give me some good link?  Aren't batch mode and console mode the same thing?

(interactivity, capabilities, switches to hide window, etc.)
0
Comment
Question by:ksuchy
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:athomsfere
ID: 33487276
Batch files are true DOS commands, about as close to the kernal as you can get, and they run right in the command line interface of Windows with little to no modification (%%a instead of %a%) is about it.

WScript is Windows, and therefore not a good standard across platforms.

CScript and JavaScript are a little better across platforms.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 33487431
Cscript vs. Wscript

The difference between Cscript and Wscript is that Cscript is the command-line version of the script host and Wscript is the graphical version. This difference isn’t really noticeable unless your script uses the Wscript.Echo command. As you’ve already seen, if you use Cscript your output from a Wscript.Echo statement will, by default, display to the command window; if you use Wscript that output will display in message boxes. Because of this, you need to consider what can happen if you change your default script host to Cscript. For example, what do you think will happen if your default script host is Cscript and you double-click the script file from Windows Explorer? Well, since the output is now going to the command prompt, Windows will open the command window, display the output, and then - here’s the … fun ... part - close the window. In other words, you’ll get a very brief glimpse of a command window that will disappear before you can actually see any of the output. Not very helpful. So be sure to keep this in mind if you decide to change the default script host.

One thing to note is that this difference in display is only with output that uses the Wscript.Echo statement. If you use the MsgBox function a message box will display whether you’re running under Cscript or Wscript; the same is true of the InputBox function.

There are other slight differences between the two, but Wscript.Echo is the most obvious and the one you’re likely to run into most often.

Further reading:

http://technet.microsoft.com/en-us/library/ee176996.aspx
http://en.wikipedia.org/wiki/Windows_Script_Host
http://www.visualbasicscript.com/Newbie-Cscript-vs-Wscript-m83649.aspx
0
 
LVL 4

Assisted Solution

by:DeadRatRacer
DeadRatRacer earned 250 total points
ID: 33491319
To add to billprew's description, cscript is often more useful when performing automation tasks using VBScript of JScript. Having a pop-up window for every echo can be tedious and make running at startup or in the background impractical.

Windows batch files are capable of using either wscript or cscript, and other than output are interchangeable.

calling a console script under the Windows Command Prompt allows you to see any output your script generates, so you dont HAVE to lose the output.  For rapid 'one off' scripts that do not require user input, console scripts are my preferred type as you can pepper it with output to aid in debugging.

Anything that the user has to interact with I think is better off using wscript to get the windows interface as opposed to what may be termed 'a clunky looking DOS-like box'.

My 2c, billprew pretty much covered the rest!

 athomsfere - I'm not really sure that kernel statement is accurate under WindowsNT+ and later or even Win95 running in protected mode. CScript is a windows program that runs vb and js scripts and is not cross-platform, in particular because almost scripting anything complicated ends up calling MS WMI, DLLs, and methods to perform tasks that are windows-centric anyway.
0
 
LVL 4

Expert Comment

by:DeadRatRacer
ID: 33491332
What was the role of MS-DOS in Windows 95?
http://blogs.msdn.com/b/oldnewthing/archive/2007/12/24/6849530.aspx

Technet script center - excellent resource!
http://technet.microsoft.com/en-us/scriptcenter/default.aspx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ksuchy
ID: 33511336
Wow!!  You guys ROCK!!  (geniuses!!)

==~~<<!!**{{  T H A N K   Y O U  }}**!!>>~~==

I have started reading the articles you linked for me and will continue to do so.

Where I work, the bosses don't ever want a msg box, or even the millisecond flash of a console window, because the philosophy is to not 'disturb' the user, and keep our work hidden in the background/transparently.  Plus, we aren't allowed to GPO their machine into straight jackets, so I can't really assume the default host (normally wscript).  If I want feedback, I pipe to text files, or hope a real programmer will have mercy on me and make an event log object with methods I can use in vbscript.

But I can't go to 200 desktops each morning and open a shell and type  
>cscript mapDriveAndExecuteInvisibly.vbs



Before I close this question, can I please ask this:

Excluding changing the default host (which my bosses would never allow), how do I ensure that my automation scripts (logon scripts, scheduled scripts, and event triggered scripts) kick off or are executed by wscript or cscript exclusively, but still ensure that I don't accidentally open a msg box, console, etc??  

0
 

Author Comment

by:ksuchy
ID: 33512278
REF: http://www.visualbasicscript.com/Newbie-Cscript-vs-Wscript-m83649.aspx

There's a SUB function listed on this page, which I guess I could keep in a separate file, and just call it on all my scripts when executed, to ensure that they restart using cscript.

As I'm not even sure how that SUB works, I wouldn't have a clue how to modify it to do the reverse (restart if not already executing using wscript), but I think this is a huge help (put me on the right tracks!).

------------------------------

One of the statements in that same 'newbie' forum thread above was:

"CScript is used when running scripts from the command line. WScript is used when running a script from the gui "

That drives me nuts.  I'm not on the computer running this.  My scripts all kick off automatically, and hopefully transparently.  Most of them are a bit opaque to my tiny brain and I didn't even make them, but I have to try to figure them out, fix or maintain them.  

Can I just clarify this much: aren't batch mode and console mode the same thing???

They refer to ANY script (cscript or wscript) that executes without the familiar windows message boxes, right???  

And the guy who said that "Wscript is used when running a script from the gui" he was really just saying that assuming wscript is the default interpreter, that double clicking a vbscript will send it to wscript, and that wscript echo's things to little windows message boxes, when not piped to text files, right ????
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33515277
==> There's a SUB function listed on this page, which I guess I could keep in a separate file, and just
==> call it on all my scripts when executed, to ensure that they restart using cscript.

==> As I'm not even sure how that SUB works, I wouldn't have a clue how to modify it to do the reverse
==> (restart if not already executing using wscript), but I think this is a huge help (put me on the right
==> racks!).

When you break it down this sub isn't too bad to understand, let me see if I can clarify a bit:

 Sub ForceCScript()
     If InStr(UCase(WScript.FullName), "CSCRIPT.EXE") = 0 Then
         Dim objShell : Set objShell = CreateObject("WScript.Shell")
         objShell.Run "%comspec% /k cscript.exe " & Chr(34) & WScript.ScriptFullName & Chr(34), 1, False
         WScript.Quit()
     End If
 End Sub

ok, the first IF statement is determining if WSCRIPT.EXE or SCRIPT.EXE is currently running this script.  It does that by referencing a system value of WScript.Fullname and then looking for CSCRIPT.EXE in it.  If it isn't found, then we need to stop running, and restart in CSCRIPT.EXE mode.  So inside the IF block, we define a handle top the Shell opbject, and then use the Run methond it provides to launch an executable.  That executes a command as if it was entered at a command line prompt and basically runs this script a second time, but this time running it under CSRIPT.  After it starts that new copy of the script it does a Quit to end this running copy, which wasn't using CSCRIPT.

Hope that helps some on that one.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33515316
Yes, I think you have the idea on the difference and purpose of cscript versus wscript.  For me CSCRIPT is a "batch" mode, or as you say console mode, where you don't get prompted for things and output is written to the console, not a pop up window.  Wscript is used for what I think of as "interactive" scripts that might prompt the user for info in a pop up box, and supply their results in a pop up window, etc.

~bp
0
 

Author Comment

by:ksuchy
ID: 33516847
Awesome, Thanks Bill!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article will help you understand what HashTables are and how to use them in PowerShell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

13 Experts available now in Live!

Get 1:1 Help Now