Guy’s Scripting Ezine 80 – ComSpec and CMD

VBScript ComSpec and CMD

This week’s Ezine describes how to script relatively simple events at the command line.  Even though the tasks are straightforward, the syntax can catch you out, unless you pay attention to the speech marks and ampersands.

 ♣

This Week’s Secret

When I want a script that will open a Dos box, I cannot make up my mind whether ComSpec is better than plain cmd.  As ever, knowledge is power, so let us first find out a little more about ComSpec.  Open the System Icon by pressing the Windows key and Pause/Break, navigate to the Advanced Tab, Click on Environmental Variables button, and lo and behold – ComSpec is listed as System Environmental Variable.  It turns out to be merely a placeholder for cmd.exe.  The nuance is that you could set ComSpec to call another program.  To digress, you could even add more environmental variables.

You may ask, ‘What is the relevance of ComSpec to scripting?’  The answer is for controlling VBScripts that employ .Run commands.  My dilemma is that SendKeys in Ezine 26 was very popular and so deservers more attention, on the other hand I worry about SendKeys because one wrong instruction and you could get disastrous results.  For example, your intention is to delete all internet temporary files.  You navigate to the desired directory and then issue *.*.  Imagine your horror if you were in the wrong directory when you issued *.* UGhhhhhhhh.

This Week’s Secret is this, by comparing plain objShell.run cmd with fancy objShell.run %ComSpec%, it literally gave me perspective on how to construct scripts which employ dos type commands.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Scenario: You want to run a command prompt

Let us pretend that this scriplet is a section in a much bigger script.  As part of the design we want to get the Command Prompt section working effectively.  The secret of success with %comspec% is to identify the three layers of command:

1) VBScript commands to create and then run the object:
Set objShell = CreateObject("WScript.Shell")
objShell.Run

2) Understanding the switches used by %comspec% or cmd:
/c  /d  /k

3) A reminder of simple dos commands
cd (Change Directory)
dir (Directory Listing)
Joining the Dos commands with ampersands (&).

Prerequisites

This script will execute on any Windows machine with WSH and where the ComSpec Environmental exists and is set to cmd.exe.

Instructions for Experimenting with DOS commands

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. One advantage of a good script editor such as OnScript is that it has a run button, which saves you time when you’re constantly adjusting the script.
  3. Save the file with a .vbs extension, for example: CreateFolder.vbs 
  4. Double click CreateFolder.vbs and check your cmd window (‘Dos box’).

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 1 – To Open a CMD or ‘Dos Box’.

This scriplet seeks to just open the command prompt.

‘======================================
‘ Comspec.vbs
‘ VBScript Comspec Example
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Ezine 80 Version 1.4 – July 2005
‘======================================

Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /k"
WScript.Quit
‘ End of Example Comspec VBScript

Learning Points

Note 1:  From a VBScript point of view see how we create a Wscript.Shell object (and not network object or an Active Directory LDAP:// connection.)

Note 2:  ObjShell has about a dozen associated methods, here we need the simple: .run. (Script Editors help you explore other methods.)

Note 3:  As ever, play close attention to the syntax, especially the speech marks.

Note 4:  Perhaps %username% is the most famous system variables, however, here VBScript employs %comspec%.

Note 5:  /k is actually a cmd.exe switch.  It means keep the command prompt window open and wait for the next instruction.

Challenges: VBScript Comspec Switches

As this week’s scripts are short and relatively straight forward, all the more freedom to experiment.

1: Substitute /c for /k.  Not very interesting here, it closes the command prompt window before you see it!  Yet in other scripts you don’t want the users to see a dos box.  Conclusion /c has its place but not in this script.

2: Here is this week’s recurring theme, swap cmd for %comspec%.  David Wanamaker explains the subtle difference between these two constructions:

%comspec% is a variable that points to the system’s command interpreter, which in most cases is cmd.exe. On a default installation of Windows, if you do an ‘echo %comspec%’ at a command line, you’ll see that it gives you the path to cmd.exe which is typically in \windows\system32. That is the reason why using ‘cmd’ instead of ‘%comspec%’ works because \windows\system32 is normally in the user’s path.

%comspec% and cmd.exe are the same thing. It’s just better in my opinion to use %comspec% because you are guaranteed that you will use the system’s command interpreter. If for some reason cmd.exe was moved to a different location and that different location is not in your path, cmd.exe would fail as a ‘command not found’ while %comspec% would always point to that different location.

Extra Information Kindly Supplied by Tom Hatfield

  1. Run regedit.exe and open HKEY_CLASSES_ROOT\Folder\shell.
  2. Create a new key called "Open command prompt here" or something similar.
  3. Under this key, create another key called "command".
  4. Set the Default string (REG_SZ) value to: %ComSpec% /k cd "%1"
  5. Open Windows Explorer, right-click on any folder, and select "Open command prompt here" (or whatever you named the key).
  6. Notice the error message.
  7. Now go back to the new registry value and changed it to: cmd /k cd "%1"
  8. Try running the command again on another folder.
  9. Notice the lack of error messages.
  10. Conclusion:  %ComSpec% and cmd are NOT interchangeable in all cases.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 2 – To Add Commands to the ‘Dos Box’.

Here is where we start to put %comspec% or cmd.exe to work.  What we want is more useful output, the ability to manipulate the ‘Dos Box’ with comspec switches.

‘=======================================
‘ ComspecDir.vbs
‘ VBScript Comspec switches
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Ezine 80 Version 1.5 – July 2005
‘=======================================

Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /k e: & dir"
WScript.Quit
‘ End of Example ComspecDir VBScript

Learning Points

Note 1:  objShell.Run "%comspec% /k e: & dir".  Firstly make sure that you have an e: drive, else change the value to d: or even c:

Note 1a: At the risk of teaching grandfathers to suck eggs, dir means list the directory.

Note 2:  Know your Dos.  For example, e:\ is ‘over think’, due to the slash the script does not work as expected.  Stick with plain e:.

Note 3:  The ampersand (&) acts as a carriage return in the Dos prompt window.  Again, you learn more if you omit the &.  The result would be a different directory listing, from what you intended.

Note 4:  Keep you eye on the speech marks.  My example has one pair at the beginning and another at the end, however, there will be other examples with nested command which require more pairs of speech marks.

Guy Challenges

Here are my challenges for you.  Add a few more instructions to the original script.

1) CMD exe has a surprising number of switches, investigate cmd /? and see the list.  For example, /d

2) Suppose you have a directory called ‘e:\logs’, try
objShell.Run "%comspec% /k e: & cd logs & dir".  Note how the ampersands (&) mimic a carriage return.

3) Add a command to pipe the output to a text file.  dir > guylogs.txt 
Also objShell.Run "%comspec% /k e: & cd logs & dir >guylogs.txt & notepad guylogs.txt"
Try /c instead of /k with the above line.

Perhaps you are bored substituting cmd for %comspec%.  (I was by this stage.)  Both act in the same way, and I declare them interchangeable.

Example 3 – To Pipe the Output to a Text File

‘===========================================
‘ ComSpecTxt VBScript
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Ezine 80 Version 1.7 – July 2005
‘=======================================

Option Explicit
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c e: & cd logs & dir >guylogs.txt "_
& " & notepad guylogs.txt"
WScript.Quit
‘ End of Example ComspecDir VBScript

Learning Points

Note the following two lines of code.  See how one command word-wraps at the end of the first line, with the help of underscore (_) and with the aid of an extra ampersand on the second line.  Also, here is an example of two non-overlapping sets of speech marks.

objShell.Run "%comspec% /c e: & cd logs & dir >guylogs.txt "_
& " & notepad guylogs.txt"

Summary of VBScript Comspec

ComSpec is ideal for adding command line code to your VBScript.  %comspec% and cmd.exe are virtually identical as far objShell.run is concerned.  For a short script there are a surprising number of factors involved, VBScript, ComSpec (or cmd.exe) and pure dos commands.

See more about VBScript syntax

VBScripts   • WMI  • Logon Scripts  • Ezine 18 Carriage returns  • Ezine 28 InStr  •Free IP Tracker  • Ezines

Ezine 47 .put  •Ezine 59 CONST  • Ezine 41 VBS Select case  •Ezine 56 VBS Select case  • Ezine 80 Comspec

Ezine 89 Sendkeys  • Ezine 97 Net Time  • Ezine 98 W32 Time  • Ezine 99 Time services  • Ezine 120 Sendkeys