COMPUTER TRAINING: DOS
Showing posts with label DOS. Show all posts
Showing posts with label DOS. Show all posts

Monday, 24 December 2012

Batch Files in DOS

Batch Files in DOS
 
What's a batch file?
 
A batch file is simply a text file that we can create by using unformatting text editor tools.
e.g. the EDIT command in MS-DOS.
 
A batch file must be named with an extension BAT.
e.g. autoexec.batmenu. bat
 
A batch file is a program which contains MS-DOS commands. Each command used in the batch file must be started from a new line and written in a correct syntax. The syntax of a command is just the same as that you use it at the DOS prompt.
 
 
 
 
 
 
Batch Files in DOS
 
How does a batch file work?
 
Now, consider the following instructions
md \newdir
copy \dos\*.exe \newdir
cd \newdir
dir
cd \
 
 
Executing commands at DOS prompt:
Normally, we can execute only one MS-DOS command at one time (except we use a "trick"). We cannot give another instruction before DOS has done our current command.
 
If we manually instruct DOS to execute the above commands, we have to type each command at the DOS prompt one after another.
 
 
Executing commands in a batch file:
However, if we put all of the commands in a text file in the same manner as in the above box, it becomes an executable program.
 
Let's name it anyname.bat Similar to a COM or EXE command, we can simply type the name of this batch file at the DOS prompt to start our instructions.
 
i.e. C:\>anyname or C:\>anyname.bat (note: the extension bat is optional here. It makes no difference, no matter we put it or not.)
 
DOS will then execute the commands automatically in the same order as written in the anyname.bat The followings are details of what DOS will do for us:-
1. Creates a new directory under the root called newdir
2. Copies all files under the DOS directory with an extension of EXE to the newly created newdir directory.
3. Changes the current directory to newdir directory
4. Display the directory listing of newdir directory
5. Changes the current directory to root directory
 
 
 
 
Batch Files in DOS
 
What commands can we used in a batch file?
 
All commands that we can use at the DOS prompt can be used in a batch file. We have to follow the same syntax of the commands as we use it at the DOS prompt.
 
For advanced application of batch programs, the following nine (9) special commands are commonly used. Some of them are used together with special characters.
 
CALLFORPAUSE
CHOICE *GOTOREM
ECHOIFSHIFT
CHOICE is an external DOS command.
 
Note:-
Please ensure the current path statement contains C:\DOS when we need to run a batch program in which the CHOICE command is used.
 
Brief Description of MS-DOS Batch Commands
CALLCalls one batch program from another.
 
CHOICEWaits for the user to choose one of a set of choices.
 
ECHODisplays messages, or turns command-echoing on or off.
 
FORRuns a specified command for each file in a set of files.
 
GOTODirects MS-DOS to a labelled line in a batch program.
 
IFPerforms conditional processing in batch programs.
 
PAUSESuspends processing of a batch program and displays the message "Press any key to continue...."
 
REMcomments (remarks) in a batch file or CONFIG.SYS.
 
SHIFTChanges the position of replaceable parameters in a batch file.
  
 
 
Batch Files in DOS
 
What are the special characters commonly used in a batch file?
 
 
Batch display-suppression operator
o @ can be applied to any command as the first character on the command line in a batch program.
o It's used to prevent an individual command from being displayed when it is executed.
 
 
Batch file label operator
o : is used to identify a location in a batch file. It is not a command but a prefix of a label.
 
o A label is a companion of the GOTO command.
i.e. GOTO <label>
:<label>
 
o A label is used to force MS-DOS to move to a specified location within the batch program.
 
o Normally, when MS-DOS runs a batch program, it executes commands in the order as they appear in the program. However, when MS-DOS comes across the GOTO command(e.g. GOTO option1), it will move to the position that is identified by :option1 within the batch program.
 
 
Batch replaceable parameter
o Consider the following commands frequently used at the DOS prompt:-
 
? copy autoexec.bat autoexec.bak
This command line creates a backup file of the autoexec.bat file.
copy is a command, autoexec.bat and autoexec.bak are parameters. These are required parameters. You must state both of them to make the command work.
 
 
? dir /w
This command line displays a directory entries of the current directory in wide screen format. dir is a command, /w is a parameter. It is an optional parameter for qualifying the output of the dir command.
 
o Similarly, we can make use of parameters to help us achieve the same capability in executing a batch program. This way, we can execute the same batch program with different data at different time. They are called replaceable parameters.
 
o %n and %%n are the special characters which represent the parameter in a batch program, where n is a single digit, from 1 through 9.
 
o The replaceable parameters are positional. The digit, n, of the special characters represents the position of the parameter we type with the batch command.
 
For example:
 
 
 
 
Batch Files in DOS
 
How can we create a batch file?
 
• EDIT command
o Usage: EDIT filename.bat
Where filename.bat is an optional parameter. We are recommended to give a name when we open the editor because it will simplify some steps in saving the file when it's done.
 
o Run EDIT command at the command prompt, we will go to an editor mode, normally in blue screen. Make sure we have saved the file upon completion.
 
o Remember to give an extension of .BAT in your batch file.
 
 
• COPY command
o Usage: COPY con filename.bat
Where filename.bat is a required parameter if we want to save the file. We must give a name when we run COPY command.
 
This method is useful when you are writing a short batch file.
o "COPY con filename"
1. Similar to backup a file using COPY command, "COPY con filename" means copying from "con" (the source) to a file called filename.bat (the target).
 
2. CON is a reserved name for console. The keyboard is known to MS-DOS as console.
 
3. By running the command, we instruct MS-DOS to copy what we type from the keyboard to a file filename.bat . When MS-DOS display a blinking cursor instead of a command prompt, it means it's ready for we to type in any text.
 
4. Remember to use Ctrl-Z (i.e. hold down the ctrl key and hit Z) or press the function key F6 to end the file. MS-DOS will display ^Z on the screen. It is the end of file marker.
 
5. And then, press Enter to save the file. MS-DOS will signal back "1 file(s) copied" and return to the command prompt.
 
 
• DOSKEY command
o Usage: DOSKEY /h > filename.bat
 
o This method is useful for a "write-and-test" approach. While we are writing a batch file, we are working on the command prompt directly. By the way, we can know the result of every command immediately.
 
o The DOSKEY program, once loaded into memory, keeps track of the commands we have typed through the keyboard.
 
o DOSKEY /h -- displays all commands stored in memory.
 
o By running the command, we instruct MS-DOS to redirect the history of commands to a batch file filename.bat .
 
o Steps in using this method:
1. Make sure DOSKEY program has been loaded into memory.
 
2. Press Alt-F7 to clear the memory buffer before we "write-and-test" our file.
 
3. Run the commands that we want to put in the batch file.
 
4. Upon completion, run DOSKEY /h > filename.bat
 
5. Run EDIT filename.bat to delete the last line "i.e. DOSKEY /h > filename.bat" and all other unnecessary lines, if necessary.
 
 
 
 

Special Features in DOS

Special Features in DOS
 
Formatting a Floppy Disk
 
When we purchase new floppy disks, we may be required to format them before we can use them. Practice formatting a floppy disk now.
 
Caution The data on the disk we format will be erased, so make sure we select a disk that does not contain information we may need later.
 
• To format a floppy disk
1. Type the following at the command prompt:
format a:
 
This command specifies that we want to format the disk in drive A. When we press ENTER, the following message appears:
Insert new diskette for drive A:
and press ENTER when ready ...
 
 
2. Insert the disk we want to format in drive A label-side up. Then close the drive door or make sure the disk clicks into the drive. When we are ready, press ENTER. The following message appears:
 
Checking existing disk format Saving UNFORMAT information
As it formats the disk, MS-DOS displays the percentage of the disk that has been formatted.
When the format is complete, the following message appears:
Volume label (11 characters, ENTER for none)?
 
 
3. A volume label is a name for our disk. We can give our disk any name we like, as long as it has 11 or fewer characters. For this exercise, type practice and then press ENTER. Information similar to the following appears:
1213952 bytes total disk space

1213952 bytes available on disk

512 bytes in each allocation unit.

2371 allocation units available on disk.

Volume Serial Number is lE49-15E2 Format another (Y/N)?
 
 
3. If we have another disk to format, press Y. If not, press N.
 
 
 
 
Special Features in DOS
 
Adding Parameters to a command
 
A parameter is a letter typed after a command that tells the command to carry out an extra task.
 
For instance, a command that we will find later in this section is the COPY command. This command copies files from one disk to another. The command with a parameter might look like this:
COPY A: MEMO.TXT C:\ /v
This partTells MS-DOS to
COPYMake a copy ...
A: MEMO.TXT.. of the file MEMO.TXT on drive A: ...
C:\... and put it on drive C:
/vand at the same time verify that
the file is recorded correctly on C:
 
 
Most parameters are optional, however you will discover, over time, ones that you use frequently.
 
 
Special Features in DOS
 
Set the Date and Time
 
Most computers on campus have an internal clock that will automatically set the date and time for us when we turn the computer on. However, if our computer does not have this feature or our computer has two floppy drives, we will need to do the following steps to set the date and time.
 
Directions:
1. Type the date like this: 1-15-97
2. Press the return key (enter key).
3. Type the time like this: 8:46
4. Press return.
5. Notice that the C:\> prompt will appear.
 
DOS marks the date and time on everything we do. It is important to periodically check the date and time if we have an internal clock to see if it is correct. If we have a two floppy system, it is important to enter the correct date and time when we turn the computer on.
 
 
 
 
Special Features in DOS
 
Check the Date and Time
 
DOS will let us to check or change the date and time once it has been set. The procedure to do this is as follows:
 
Directions:
1. Type: date and press return.
2. If the correct date is displayed, simply press return. If the date is incorrect, type the correct date and press return.
3. Type: time and press return.
4. If the correct time is displayed, simply press return. If the time is incorrect, type the correct time and press return.
 
 
 
 
 
 
Special Features in DOS
 
Change the Default Drive
 
To change from our hard drive to a floppy drive, we will need to indicate this change as follows:
 
Directions:
1. Put a floppy disk into drive A.
2. Type: a: and press return.
3. Your prompt should change to A:\>
4. Type: dir and press return.
5. You are now able to see all the files on the floppy.
6. To change back to your hard drive, you will need to reverse this procedure.
7. Type: c: and press return. Now your prompt should be C:\>
 
 
 
 
 
Special Features in DOS
 
Diskcopy Command
 
Sometimes there is a need to copy all the files on one floppy diskette to another floppy. First we will need a blank formatted disk.
 
Directions:
1. Type: diskcopy a: a: and press return.
2. When asked to put in the source disk, put in the diskette that has the information we want to copy into drive A and press return.
3. Wait a few seconds. When asked to insert a target disk, take out the diskette from drive A and insert the blank floppy disk and press return.
4. Once this process is complete, we will be prompted to write to another duplicate diskette(Y/N)Press N.
5. Then we will be prompted to copy another disk (Y/N)? Press N.
 
NOTE:
If our machine has two floppy disk drives, insert the original (source) disk into drive A and the blank (target) disk into drive B. Then type: DISKCOPY A: B:
 
 
 
 
Special Features in DOS
 
Copying a File from the Hard Drive to a Floppy Disk
 
Sometimes there is a need to copy a file from the hard drive onto a floppy disk. This is helpful if we would like to have an extra copy of a certain file as a backup or to transport the copied file to another computer.
 
Directions:
1. Type: copy <insert filename here> a: and press return.
2. Wait for a few seconds, notice the red light on the floppy drive. Never attempt to remove a disk from the disk drive while this red light is on.
3. To check to see if the file was copied, type a: at the C:\> prompt and press return.
4. Type: dir and press return.
 
 
 
 
 
 
Special Features in DOS
 
Comparing two diskettes
 
We use the DISKCOMP command to check that two diskettes are EXACT copies of each other.
 
NOTE: -
They MUST both be of the SAME capacity i.e. 720Kb OR 1.44Mb
 
It is usually used immediately after using DISKCOPY command to ensure that both copies are the same. DISKCOMP refers to the original diskette as the first diskette and the copy as the second diskette.
 
If our computer has one diskette drive ( this is still normal!!)
 
1. Type
DISKCOMP A: A: {ENTER}
 
2. When this message appears:
Insert FIRST diskette in drive A:
Press any key when ready...
 
Insert the original diskette in drive A: and then press any key.
 
3. When this message appears:
Insert SECOND diskette in drive A:
Press any key when ready...
 
Remove the original diskette from drive A: and insert your copied diskette into drive A: and press any key again.
 
4. When this message appears:
Compare OK
Compare more diskettes (Y/N) ?
 
Type Y (for yes) to compare another diskette, N (for no) to exit from the DISKCOMP command.
 
NOTE: -
If the diskettes are NOT identical run the DISKCOPY and DISKCOMP commands again.
 
 
 
 
Special Features in DOS
 
Checking the Condition of a Disk
 
We use the CHKDSK command to:
• Check a disk to see how much space there is available, and how much is in use.
• Fix some disk errors, such as files that show a non-zero size but that really have no data in them
• Display information about the disk, including the total disk space, the number and size of files.
• Display how much Random Access Memory (RAM) is available to MS-DOS( This is NOT the total memory in our computer!!)
 
Run CHKDSK occasionally on all our disks to check for errors.
 
The command:
CHKDSK A: /F {ENTER}
Checks the disk in drive A: for errors and fixes any errors it can.
 
If CHKDSK finds no errors it displays a report like this:
1213952 bytes total disk space
 
87040bytes in 3 hidden files
 
4608bytes in 6 directories
 
1078784 bytes in 97 user files
 
43520bytes available on disk
 
655360 bytes total memory
 
588480 bytes free
 
If the CHKDSK command reports any errors on the disk, it displays a message such as:
xxx lost clusters found in xxx chains and asks you whether you want to correct the errors.
Type Y (for yes) and the press the {ENTER} key.
 
NOTE:-
This procedure can cause you to lose some of the information on your disk, however, not fixing the disk can cause you to lose even more!!
 
 
 
 
Special Features in DOS
 
Displaying the contents of a file
 
The TYPE command enables us to display the contents of a file on our screen.
 
NOTE:
There are some files, for example, operating system commands or applications software, which we may not be able to read because they contain non-alphabetic and non-numeric characters.
 
This command:
TYPE APRIL.DOC {ENTER}
Displays the contents of the file APRIL.DOC.
 
If we want to display a long file, we can use the MORE command to display information one screenfull at a time.
For example:
TYPE APRIL.DOC | MORE {ENTER}
 
Type pauses and displays the prompt --- More --- at the bottom of the screen, to view more, press any key.
 
NOTE:
The pipe ( | ) switch can usually be found by holding down the SHIFT key and pressing the backslash ( \ ) key.
 
 
 
Special Features in DOS
 
Wildcards
 
Wildcards are symbols that enable us to perform an MS-DOS operation on more than one file at a time. A file specification that contains wildcards can refer to more than one file because it gives MS-DOS a pattern to match.
 
MS-DOS searches for any file whose filename or extension matches the pattern. There are two wildcard characters:
 
?A question mark in a filename or extension means that up to a maximum of ONE character/letter/number can occupy that position.
*An asterisk in a filename or extension means that any number of characters/letters/numbers, (up to maximum of EIGHT before the dot, and up to a maximum of THREE after the dot) can occupy that position.
 
NOTE:-
There can be LESS than the maximum number of characters/letters/numbers.
 
For example suppose you had a directory containing the following files,
 
A.DOC
AA.DOC
AAA.DOC
B.DOC
A.TXT
B.TXT
 
The following command and file patterns match some or all the above files:-
DIR *.DOCIs the equivalent to DIR ????????.DOC and matches the first four files (those with the DOC extension)
 
DIR *.*Is the equivalent to DIR ????????.??? and matches ALL files. Use *.* with care e.g. DEL *.* deletes ALL files in the current directory, regardless of extension!!
 
DIR ?.DOCMatches A.DOC and B.DOC
 
DIR ?.*Matches A.DOC, B.DOC, A.TXT and B.TXT
 
DIR A?.DOCMatches A.DOC and AA.DOC
 
DIR A*.DOCMatches A.DOC, AA.DOC and AAA.DOC
 
 
 
 
 
Special Features in DOS
 
Help
 
It can be used in two ways:
1) HELP {ENTER}
2) HELP {command help needed on} {ENTER}
 
The first way will invoke the full list of MS-DOS commands that we can scroll through using the cursor keys and the TAB key, or point and click the command we are interested in for the opening screen and further details.
 
The second method will take you straight to the commands opening screen of information, usually there are links to an Example page and a Notes page. The opening screen will give you the syntax for the command, for example the DIR command:
 
Syntax
DIR [drive:][path][filename] [/P] [/W][/A[[: ]attributes]][/O[[:]sortorder]] [/S] [/B] [/L] [/C]
 
There then follows detailed explanations of what each parameter and switch does. A shortcut to find the parameters and switches available for most MS-DOS commands and many program names, is to add the switch /? after the command name. Even WIN the command to start MS Windows will give a list of possible switches that you could use.
 
 
 
 
Special Features in DOS
 
Input/Output Redirection and Filter Commands
 
So far, we might have learned how to use some commands in their standard form, e.g. to display the content of a text file, to view a directory listing or to view the tree structure of directories. We can view the output directly on the screen.
 
Using MS-DOS command in standard form, we type in our instruction at the command prompt, through the keyboard. It's a standard input.
 
Then, MS-DOS will process your input and display the result on the screen. It's a standard output. Up to this point, the job is simply done.
 
Now, if we want to record a directory listing for further reference or get a hard copy immediately, we need to instruct MS-DOS not to display the result on the screen but to save it to a file or print it out directly. It's called output redirection.
 
On the other hand, if we want to process a job by using data on an existing file, we need to instruct MS-DOS to use data from a given file. It's called input redirection.
 
In some cases, moreover, we may need to process a job by executing more than one command. By the way, the output from the first command will not be displayed on the screen but to be redirected into the input of the next command. It involves both output and input redirection. The logical connection betwwen these two commands is called piping.
 
Piping is associated with filter commands:
1. find,
2. sort and
3. more.
 
 
They are called filter commands because they work much like a filter in a water system, which takes incoming water, changes it in some way, and sends it along the system (a good explanation from Van Wolverton's Running MS-DOS).
 
The application of these filter commands are listed as follow. They can be used simply in an input redirection or through a logical piping connection.
• Find searches input data for a text string.
• Sort arranges input data in order.
• More displays output one screen at a time.
 
The following special characters are needed in Input/Output redirection and Piping:
 
 
Examples