MS-DOS

Redirecting Command Input and Output

 

Usually DOS receives input from the keyboard and sends its output on the monitor. Fortunately DOS provides the facility of redirecting this flow of command input and output by using redirection symbols. Using redirection symbols, you can receive data from a file instead of keyboard and you can send your data to a file or a printer instead of monitor. With redirection symbols, you can also create pipes that let the output from one command become the input for another command.

Redirecting Output – As stated earlier most commands send output to your monitor. If you want to alter this and want to send the output to a file, you will use an output redirection symbol ‘>’ (greater-than) in your command. For example,

C:\>dir > listings

Sends the output of dir command to a file named listings. If the listings file does not exist then it creates first and then stores your directory contents there. And if the listings file exists then it wipes out its previous contents and stores new data in it.

Appending Output – However if you want to append this new data to the end of a specified file then you will use an append symbol ‘>>’ symbol  (two greater-than). This symbol tells the DOS to append the output of a command to the end of a specified file. Thus the following commands

C:\>dir >> listings

appends your directory listing to an existing file named listings. Unfortunately if listings does not exist then DOS creates it.

 

Redirecting Input – Like this you can also receive input for a command from a file instead of from the keyboard using an input redirection symbol. For example,

C:\>sort < names

sorts the contents of the specified file names and displays the result on the monitor. One should remember that both input and output redirection symbols can be used in a single command as:

C:\>sort snames

sorts the contents of the specified file names and sends the result of sort command to the specified file – snames instead of displaying the result on the monitor.

 

Filters and Pipes

 

A filter is a special type of program that reads your input, transforms it in a special way and then outputs it to the monitor. DOS has three types of filters – find, more and sort.

find – this filter is used for searching a text in a specified file

more – this filter is used to display the contents of a file one screenful at a time

sort – this filter is used to sort the contents of a file alphabetically

DOS also provides the facility of redirecting the output from a filter into a file, or using it as input for another filter by using pipes. Pipes are normally used when you want to provide the output from one command as the input of another. Piping is done by separating commands with the pipe ‘|’ (vertical bar) symbol. For example, in the following command the pipe sends all output generated by the dir command as the input to the sort command

C:\>dir |  sort

Fortunately DOS provides the facility of using pipes with I/O redirection symbols. Thus

C:\>dir |  sort > fnames

stores the sorted listing of directory contents to the file fnames. However you can also use more than one pipe symbol on a command.

 

Configuring Your System (config.sys)

 

This section describes how to configure your system using a configuration file. The configuration file, that is config.sys, is a file that you can use to set up MS-DOS. The config.sys file is essential to your system, unfortunately yet many users have no idea what its commands do.

When you start DOS it loads its core program files into memory & then it automatically searches for a file named - config.sys, which contains certain commands that let you set up (or you can say configure) DOS for use with devices or application programs. Using config.sys file, you are allowed to change the default configuration setting of the computer system. Fortunately if config.sys file already exists then you may add some other commands in the file. For example, you can add installable device drivers to your system by including special commands in your config.sys file. And if the config.sys file does not exists then you can use MS-DOS editor to create this config.sys file and then save it on the root directory of the drive.

 

Config.sys Commands – As stated earlier, config.sys file contains special configuration commands. One main point to note about these commands is that these are unique to config.sys; you can not use them anywhere else. Nor you can use ordinary commands, such as a dir or copy in config.sys. Here are some more commonly used configuration commands:

 

  • break – sets the Ctrl+C check.
  • buffers – sets the number of sector buffers in memory.
  • device – installs the device driver in the system.
  • drivparm – specifies parameters for block devices when you start MS-DOS, overriding the original MS-DOS device driver setting.
  • files – sets the number of open files.
  • install – executes special DOS commands during the processing of the config.sys file.
  • lastdrive – sets the maximum number of drives you may access at any time, letters from a to z.
  • rem – inserts comments to the config.sys file.
  • shell – begins the execution of the DOS-shell from a specific file (usually command.com) located in the specific directory
  • stacks – supports the dynamic use of data stacks.

 

The following is a sample config.sys file:

break    = on

buffers  = 50

lastdrive = g

shell     = c:\dos\command.com c:\dos /p

device = c:\dos\ansi.sys

This example includes five types of commands: break, buffers, lastdrive, shell & device. Here the first command, break = on, makes it possible to use Ctrl+C in real mode to stop a program, depending upon the program that you are running. Next command, buffers = 50, sets the total number of buffers in memory to 50. However, if your directory system is large enough then you may increase the number of buffer. The next command, lastdrive = g, sets the last available logical or physical drive to g. The letters from a to z are available as labels for logical or physical disk drives. Next we have the command, shell = c:\dos\command.com, which begins the execution of the shell from a file defined in the command.com file located in the dos directory on the disk in drive C. Finally we have device = c:\dos\ansi.sys, which causes DOS to replace all keyboard input & screen output support with the ANSI escape sequences. These are all commands that you can use only in config.sys.

Here one should remember that DOS performs the commands in config.sys only when you start the system. Thus whenever you want your new changes to take effect then you must restart DOS after editing this file.

 

Batch Files

 

Batch files are used when you need the same sequence of commands over & over to perform some special task. Actually a batch file is just a set of commands that the computer can execute automatically as a group or a batch instead of one at a time. Using batch file, you run the whole sequence of commands by simply typing the name of the batch file. In effect, you use batch files to create your own personalized commands. Therefore you can say that batch files can multiply the power of DOS.

Note that when you use batch files, you do not need to type the batch file extension, even though all batch files must include .bat extension in their filenames. You can create a batch file by using DOS Editor.

Before running a batch file you must know a few things, such as::

 

  1. Save a batch file with an extension .bat.

2.  To execute a batch file, simply type its filename only and not the extension.

3.   Press Ctrl+C keys simultaneously only when you want to stop the processing of batch file, DOS asks you to confirm that you want to terminate batch

process.

4.  DOS prompts you to reinsert the disk, if you remove the disk that contains a batch file being run, so that it can continue the processing of file.

  1. You can also use a batch file within another batch file by placing the name of another batch file as the last command in a batch file.
  2. You can also use I/O redirection symbols, such as ‘’, ‘’, & a pipe (|) in a batch file.

 

When DOS executes a batch file, it carries out the commands in the order they appear in the file. During the execution of a batch file, for each command, DOS displays a blank line, displays the command prompt, fills in the command, & then executes it. DOS offers several commands that are particularly useful in batch files. The rem, echo, cls pause & call commands are almost always used in batch files rather than at the command prompt.

The cls command clears the contents of the screen & puts the cursor in the top left corner. The rem command creates comments in your batch files. This command helps document what a program does. The echo command turns command echoing on or off. Note that DOS does not echo commands that follow echo off in a batch file. The pause command, which has no parameter, stops the progress of the batch file until you press a key.

For example, the batch program tempdel.bat may look like this:

 

@echo off

del *.bak

del *.tmp

del temp.*

echo All unnecessary files are deleted. Press any key to continue.

pause

When you run this program, it deletes three types of files from the current directory & then it echoes the message & the pause command tells you to press any key when ready.

You can use call command to start a batch program from another batch program. Of course you can execute another batch program within another by just using its name, but it offers some limitation. For example, let you want to use tempdel.bat from within valuedel.bat as follows:

@echo off

tempdel

cls

The limitation in this is that when you run one batch file from another, DOS transfers to the second program, but unfortunately it would never return to first one. Thus here once the control is transferred, the remaining commands in valuedel are never executed. To overcome this limitation we use call command as:

 

@echo off

call tempdel

cls

Now when we run valuedel command, the call command executes another batch file, tempdel, without completely transferring to it. In this case, DOS returns to the original program afer finishing the called program.

One main point to note here is that if you have more than one command with the same name, then it is obvious that only one file would be executed. In such case, DOS will execute only one of them, according to the following order of precedence - .com, .exe, and .bat. For example, suppose there are two files – xfile.com and xfile.bat. If you type the command – xfile, and press Enter key then DOS would always run the program xfile.com first. To run the batch file xfile.bat, you would have to place it in a different directory & give a path along with the external command.

 

The autoexec.bat File

DOS has a special batch file – autoexec.bat. The autoexec.bat file runs programs automatically when you start DOS. The autoexec.bat file can be very useful when you startup your computer system under DOS and you want it to run a batch program each time your computer system is started.

When you start DOS, it searches the root directory of the default disk drive for a file named – autoexec.bat. If autoexec.bat exists then DOS immediately executes it, bypassing the date and time prompts. And if it does not exist then the date and time prompts appear automatically. Note that DOS does not prompt you for a current date & time unless you include the date & time commands in your autoexec.bat file.

You can create an autoexec.bat file by using DOS Editor. However you can also create a file using a copy command. Type the following copy command and then press Enter key:

copy con autoexec.bat

Here the copy command tells DOS to copy what you type from the keyboard into the file autoexec.bat. Let us understand the concept of autoexec.bat file by taking following commands in your autoexec.bat file

 

date

time

path = c :\ ;  a:\ ; b:\

prompt = $p$g

cls

In this sample file, the date and time commands ask you to set the date and time each time you start MS-DOS. Next command, path = c:\; a:\; b:\, tells MS-DOS to look for commands or programs from ‘a’ & ‘b’ drive in addition to ‘c’ drive. After the path command, we have the prompt command, prompt = $p$g, which tells DOS to display the default drives and directory followed by a greater–than symbol (>). Finally the cls command clear the contents displays the listing default directory of the disk in the default drive. Your system may have some different contents in autoexec.bat file. It depends upon one’s requirement.

 

Now one may ask – If both the config.sys file and autoexec.bat file are special system files and both are MS-DOS files and both are executed when you start your system then where do they differ?.Actually there is a subtle difference between these two files. The autoexec.bat is a batch file that contains any set of commands or programs that you would normally want to run when you start the computer. On the other hand, the config.sys file may contain only a special set of configuration commands. Another minor difference is that if you modify the commands in the config.sys file, you must restart your computer system, whereas in case of the autoexec.bat you simply type the word autoexec & press Enter key.

Another person may ask you – Which file executes first either autoexec.bat or config.sys? Answer is DOS firstly checks a config.sys file then it loads command processor and in last command processor searches for autoexec.bat file. In other words, after carrying out all commands in config.sys, DOS looks for & runs autoexec.bat. After executing autoexec.bat DOS turns control over to you, either from the command prompt or from a program such as DOS Shell or Windows.

 

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments