DOS Scripting Tips and Tricks

Two Command Processors

In general there are two command processors, 16-bit and 32-bit. These are command.com and cmd.exe respectively. You can find cmd.exe inside the %SystemDrive%\Windows\System32 folder.

So, what's the difference between cmd.exe and command.com?

command.com has the same purposes as cmd.exe except that it only supports 16-bit programs. In addition, it does not support .cmd files and has fewer built-in commands and is more limited in its syntax (cmd is a newer, more modern, more advanced command-line interpreter).

It should be noted that 64-bit versions of Windows have completely dropped support of 16-bit programs. However there is work around to run 16-bit programs on Windows 64-bit machines, for example, using the Dosbox tool but that's beyond the scope of this article.

So, what's the difference between .bat and .com files?

There is a subtle difference that I noticed in bat and com files in terms of execution. For example, if you have TEST.BAT and TEST.COM files in the same folder and at the command prompt type TEST, then it'll execute the TEST.BAT file. This means that .BAT files take precedence over .CMD files when two versions of the same file name exist in the same folder. The reason is PATHEXT environment variable lists the .BAT extension before the .CMD extension in its list.

Try the following: echo %pathext%

Echo

Tips and Tricks

Function keys

F3: Pastes the last executed command
F7: Displays a selectable list of previously executed commands

List of Executable Commands

F9: Asks for the number of the command from the F7 list to paste

Command Number

If you enter a command number as 11 then it'll paste echo %pathext%

PROMPT Command

Do you know why a command prompt starts like this:

C:\Users\<user name>

It depends upon the PROMPT command set. So execute the following command:

echo %prompt %

Output

$P$G

Now you can change this setting as your wish. For example, I prefer the Unix style, execute:

prompt $$

Output

Prompt Output

Operators

  • Redirection operators are my favorites, these are > and >>

    execute systeminfo > c:\mycomputerinfo.txt

    Output

    A text file gets created. You can see the content by executing the following command:

    start > c:\mycomputerinfo.txt

    Output



    The other operator is useful when you want to append to any existing file, for example execute the following:

    help >> c:\mycomputerinfo.txt

    Output

    It'll append the HELP command output into an existing c:\mycomputerinfo.txt.

    You can execute start > c:\mycomputerinfo.txt to see the content again.

  • Pipe Operator: Feed the output of one command into another command, the most common use I do is:

    Dir | more

    Output

    Directory Info

Title Command

Bored of looking at the command prompt? Execute:

title C-SharpCorner

Output

Title Command

Please share your feedback.