Wednesday, 22 January 2014

Batch File Concepts

BATCH FILE:
        

                       These are simple text files containing some lines with commands that get executed in sequence, one after the other. These files have the special extension BAT or CMD.

Files of this type are recognized and executed through an interface (shell) provided by a system file called the command interpreter. In Windows XP/ Vista the command interpreter is the file cmd.exe.

The large assortment of versatile commands available in Windows XP/Vista/7 makes batch files a powerful tool.Constructing a batch file consists of nothing more than opening any text editor like the accessory Notepad, entering some lines containing commands, and saving the file with an extension BAT or CMD.


Basic Commands Are,


@echo off - The purpose of this first command is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "@" in front makes the command apply to itself as well.

Rem - Very often batch files contain lines that start with "Rem". This is a way to enter comments and documentation. The computer ignores anything on a line following Rem.

set /p variable= [string] - "Variable" is the name of the variable that will be assigned to the data that we want the user to input. "String" is the message that the user will see as a prompt. If desired, "string" can be omitted.

Here is an example that asks the user to enter his or her name:

            set /p name= What is wer name?

This will create a variable %name% whose value is whatever the user enters. Note that the user must press the "Enter' key after typing the input.


Conditional branching with "If" statements:

Batch files can make decisions and choose actions that depend on conditions. This type of action makes use of a statement beginning with "If".

The basic meaning of an "If" statement is ,

        If something is true then do an action (otherwise do a different action)

The second part of the statement (in parentheses) is optional.

Otherwise, the system just goes to the next line in the batch file if the first condition isn't met.

The actual syntax is ,
   
        If (condition) (command1) Else (command2) The "Else" part is optional. The form "If not" can also be used to test if a condition is false.

Note that "If" tests for true or false in the Boolean sense.

"If exist" statement:

There is a special "If exist" statement that can be used to test for the existence of a file, followed by a command.

An example would be:

        If exist somefile.ext del somefile.ext

 we can also use a negative existence test:

        if not exist somefile.ext echo no file


"If defined" statement:

Another special case is "if defined ", which is used to test for the existence of a variable.

        For example: if defined somevariable somecommand

This can also be used in the negative form, "if not defined".


When comparing variables that are strings, it may be best to enclose the variable name in quotes.

For example, use:

        if "%1" == somestring somecommand


 Variables are declared and given a value in a single statement using "set".

  The syntax is: set some_variable = some_value




Localizing variables:

The declaration of a variable lasts as long as the present command window is open. If you are using a batch file that does not close its instance of the command window when the batch file terminates, any variables that the batch file declares remain.

If you wish to localize a variable to a particular set of statements, use the "setlocal" and "endlocal" commands. Thus. to confine a variable declaration to a particular block of code, use:....


                setlocal
           
                set some_variable = some_value

                ...some statements

                endlocal

                ...

No comments: