The power of CMD.EXE commands
The Tech forum
todays world of Wizards, Automated Installation Programs, object oriented
programming and GUI-based applications, do you still remember the DOS prompt?
Of course on the Windows OS now, it is replaced with CMD.EXE. But do you still
remember the batch files we used to write? Do you still write some batch files?
Whether you do or you dont I am sure you will find this content very useful
While exploring the online help available for these commands, we came across
many gems which, we felt, were worth sharing with all of you.
What is the relevance of this in todays world? The idea is simple
we often tend to do complex, multi-step, GUI-based tasks, which could have been
completed in a fraction of time if you knew the nuances of these commands.
So read on.
Just to start off on the right track, start a command prompt by typing cmd in
the Run command dialog.
cmd /? > learn.txt
What does this do? The /? Parameter outputs help about CMD itself. The >
sign outputs it to learn.txt. Now open this text file and browse
through it in Notepad.
A quick glance will tell you that there is lot of functionality and in-depth
thinking in something as ignored as CMD.EXE
Please notice that it supports Unicode something you may not even have
expected it to do.
The bottomline is that although the CMD.EXE may behave like good old DOS, it
is much more than that.
In the Start Run dialog, we are used to just specifying URLs or Filenames.
Windows is able to understand which application should open these by default.
For example if you type sample.doc, Windows finds out that you need
to actually start Word and pass this filename as a parameter to it.
In CMD if you do this, it would simply say sample.doc. It will start
Word exactly like it did in Windows Run command.
In some cases, like URLs this may not work by default. To make even a URL work
as expected, type the command as follows:
Now, you will think that is all there is to the start command. You are thoroughly
Just for your info, here is what you get when you type
that it says Press any key to continue
I did. It shows 4 more screens of information about a single command
Start! Check out how much you are missing. Anyway, I leave it to you to explore
the Start command options.
One great thing to note that this command allows you to control the process
level execution priority right at the outset. Usually when we run Windows commands
from the Windows UI, we first run the applications and then change it using
task manager (I hope you know how to do this!)
Another thing to know is when to run which command in which priority. Unfortunately,
if I do this here, we will go into another big topic and never return to the
CMD.EXE. So I will leave it till some future article.
Executing multiple commands together without writing a batch file
Suppose you want to run few commands one after the other. But each command takes
a long time to complete. What do you do? You type first command, wait for it
to finish, then type the next command and so on.
Of course you could have written a batch file. But then you dont want
to re-execute these commands in future. So why waste the time in writing a batch
Here is a simple way of achieving this.
Suppose you want to run two commands one after the other, but you want to type
them all at once without writing a batch file. The commands are, for
example, dir then cd .. and then echo %path%
Here is how you go about doing it.
Type the opening bracket first and then press the Enter key. The system will
prompt you for more. Now on each line type a new complete command and press
Enter for the next command.
Finally, write the closing bracket and press Enter. Thats it.
If any of the command generates error, the further commands will still run.
This may be a problem in some cases. If you want commands to run only if the
prior one has run without an error, there is a way.
from the previous section, what if you want conditional execution of commands?
First of all how do you know if any command generated an error or completed
There is an environment variable called errorlevel which contains this information.
This variable contains a value which is updated based upon the execution of
the last command. If there was no error the value is zero. If there was a problem
the value will be non-zero. It is possible to put a custom number into the errorlevel
variable to indicate specific type of error conditions. Usually, the value is
1 if there is any error.
Now, we have a command called If which can check the value of errorlevel after
the previous command execution and then run the next command.
But there is a much simpler way available.
Suppose you want to run the first command DIR and then if DIR is successful,
run another command called Echo Successful. How do you do this?
Simply type the following:
Dir *.* && Echo Successful
The && operator allows us to have conditional execution capability.
The command && executes only if the command before it runs successfully
(errorlevel = 0).
Now, you may want to handle the failure of the first command by running a third
command. How do you do this? That is also thought of !
Dir fdsafs && echo successful || echo failed
Note that the pipe symbol ( | ) is used twice. Here I have used a filename which
did not exist in the directory. Therefore, the DIR command fails with a File
not found error. Therefore, the echo failed command executes.
This is how, we have a very powerful if then else kind of functionality.
Now let us take a look at a more usable and practical example. This example
assumes Windows XP as the OS. Please note, the text marked in bold
is not to be typed. These are prompts from the OS. Please note, the commands
continue on a single line (without pressing Enter) between the More prompts.
C:\Documents and Settings\nitin>xcopy c:\upload\*.* \\backupserver\aug2004
More? eventcreate /t information /id 999 /l Application /d Copy succeeded
/so my batch copy process
More? ) || (
More? eventcreate /t error /id 998 /l Application /d Copy failed. Please
retry /so my batch copy process
More? net send administrator Batch copy has problems.
What are we doing here? We are running a commonly run, long, batch process here.
If it succeeds or fails we are logging an event into the event viewer and then
sending a command to administrator if it fails.
Note that with failure we have used two commands. Impressive, is it not?
Now, how did we log errors in the eventviewer? Did you know about the command
called EventCreate? Yes. This is also one of the many powerful command line
utilities available with Windows XP and Windows 2003.
More on such useful commands next time.
the Author:Dr Nitin Paranjape is the Chairman and MD of Maestros (Mediline).
He is a consultant with many organisations, covering appropriate technology
utilisation, business application of relevant technology, application architecture
and audit as well as knowledge transfer. He has authored more than 650 articles
on various technology-related subjects. He can be contacted at email@example.com