Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Ezine 80 - 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.
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.
For a top Script Editor try a free download at OnScript
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 (&).
PrerequisitesThis 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
- Copy and paste the example script below into notepad or a VBScript editor.
- 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.
- Save the file with a .vbs extension, for example: CreateFolder.vbs
- Double click CreateFolder.vbs and check your cmd
window ('Dos box').
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
This scriplet seeks to just open the command prompt.
'======================================== ' Comspec.vbs ' Sample VBScript ' Author Guy Thomas http://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 PointsNote 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 we employ
%comspec%. Note 5: /k is actually a cmd.exe switch. It means keep the command prompt window open and wait for the next instruction. ChallengesAs 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.
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' from a script.
'======================================= ' ComspecDir.vbs ' Sample VBScript ' Author Guy Thomas http://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 PointsNote 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 ChallengesHere 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.
'=========================================== ' ComSpecTxt VBScript ' Author Guy Thomas http://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 PointsNote 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"
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|