Friday, January 28, 2011

Objective:



 


What's a batch file?   [ Top ]
  • A batch file is simply a text file that you can create by using unformatting text editor tools,  e.g. the EDIT command in MS-DOS. (some other methods will be discussed later)

 
  • A batch file must be named with an extension BAT, e.g. autoexec.bat, menu.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.

  How does a batch file work?   [ Top ]
Now, consider the following instructions for discussion.
 
md \newdir  
copy \dos\*.exe \newdir  
cd \newdir  
dir  
cd \

  • Executing commands at  DOS prompt:
Normally, you can execute only one MS-DOS command at one time (except you use a "trick"). You cannot give another instruction before DOS has done your current command.
If you manually instruct DOS to execute the above commands, you have to type each command at the DOS prompt one after another.
 
  • Executing commands in a batch file:
However, if you 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, you can simply type the name of this batch file at the DOS prompt to start your instructions. i.e. C:\>anyname  or  C:\>anyname.bat  (note: the extension bat is optional here. It makes no difference, no matter you 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 you:-
    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

What commands can you used in a batch file?    [ Top ]
  • All commands that you can use at the DOS prompt can be used in a batch file. You have to follow the same syntax of the commands as you 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 (details in next section).

 
* CHOICE is an external DOS command.  
Please ensure the current path statement contains C:\DOS when you need to run a batch program in which the CHOICE command is used.

(click on command names to view the function and syntax of each command
or click here to view the brief description of them.)


What are the special characters commonly used in a batch file?    [ Top ]
 
Special characters
@
:
%
%%
Name:
batch display suppression 
operator 
 
batch file label operator
batch replaceable 
parameter
batch replaceable 
parameter 
 
Syntax:
@<command> 
where command is any MS-DOS command 
 
:<label> 
where label is a string up to 8 characters long
%n 
where n is a single digit 1 through 9
%%n 
where n is a single digit 1 through 9
Example:
@echo off 

:option1
%1, %2, %3 etc.
%%1, %%2 etc.
Remarks
--
--
--
used with FOR command only
 
  •  Batch display-suppression operator

 
    • @ can be applied to any command as the first character on the command line in a batch program.
    • It's used to prevent an individual command from being displayed when it is executed.
 
  • Batch file label operator

 
    • :  is used to identify a location in a batch file. It is not a command but a prefix of a label.
    • A label is a companion of the GOTO command.

i.e. GOTO <label>
      :<label>
    • A label is used to force MS-DOS to move to a specified location within the batch program.

 
    • 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

 
    • 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.
 
    • Similarly, you can make use of parameters to help you achieve the same capability in executing a batch program. This way, you can execute the same batch program with different data at different time. They are called replaceable parameters.
 
    • %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.
 
    • The replaceable parameters are positional. The digit, n, of the special characters represents the position of the parameter you type with the batch command.

For example:
 
A sample batch file named 
greeting.bat
Batch file executed in 
DOS prompt
@echo off 
echo %1 is %2 
echo
c:\>greeting IOS100 fun 
IOS100 is fun     (<-- i.e. the output) 
 

Replacement of % n by given parameters:
batch filename (a.k.a. %0)
greeting
%1
IOS100 
%2 
fun 


How can you create a batch file?    [ Top ]
  • EDIT command
    • Usage: EDIT filename.bat

where filename.bat is an optional parameter. You are recommended to give a name when you open the editor because it will simplify some steps in saving the file when it's done.
    • Run EDIT command at the command prompt, you will go to an editor mode, normally in blue screen. Make sure you have saved the file upon completion.
    • Remember to give an extension of .BAT in your batch file.
 
  • COPY  command
    • Usage: COPY con  filename.bat

where filename.bat is a required parameter if you want to save the file. You must give a name when you run COPY command.
    • This method is useful when you are writing a short batch file.
    • "COPY con filename"
      • 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).
      • CON is a reserved name for console. The keyboard is known to MS-DOS as console.
      • By running the command, you instruct MS-DOS to copy what you 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 you to type in any text.
      • 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.
      • 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
    • Usage: DOSKEY /h > filename.bat
    • This method is useful for a "write-and-test" approach.  While you are writing a batch file, you are working on the command prompt directly. By the way, you can know the result of every command immediately.
    • The DOSKEY program, once loaded into memory, keeps track of the commands you have typed through the keyboard.
    • DOSKEY /h -- displays all commands stored in memory.
    • By running the command, you instruct MS-DOS to redirect the history of commands to a batch file filename.bat .
    • Steps in using this method:
      • Make sure DOSKEY program has been loaded into memory.
      • Press Alt-F7 to clear the memory buffer before you "write-and-test" your file.
      • Run the commands that you want to put in the batch file.
      • Upon completion, run DOSKEY /h > filename.bat
      • Run EDIT filename.bat to delete the last line "i.e. DOSKEY /h > filename.bat" and all other unnecessary lines, if nece

No comments:

Post a Comment