Basic Batch File Commands

We are done with all the basic preliminary concepts like variables, operators, conditional, branching and looping commands. Now this tutorial deals with the basic batch file commands that are used generally to make a batch file more complicated.

Sub commands, Switches, Parameters

Before jumping into the commands, I’d like to give a brief overview of the parts of commands. A command in batch program may consist of sub commands and switches. The sub commands are the supportive commands that are accompanied with the main commands. These sub commands generally serve to improve the accuracy of the result that we are looking for. Like the below example,

This command is used to list all the users on a computer. As you can see here, ‘net’ is a main command and the sub command ‘users’ is used to narrow down the results.

Switches allow us to run different variations of commands. You might have seen the switches in the ‘set’ command like p,a. 

We can also pass parameters to the batch file which are nothing but the command line arguments. These parameters can be accessed by a number each in order. The syntax to pass the parameters from the command line is as shown below.

Eg: new HelloWorld!

Here ‘new’ is the batch file name and ‘Helloworld!’ is the argument that I will pass through the command line to the program. The parameters are delimited by spaces and commas, if I had given a space or a comma, Hello and world will be treated as two different parameters. So, here hello and world are treated as single parameter. Try out the below program to access the parameters through command line.

Program #1: To access the parameters given from command line


If you see the program you can easily say that the program prints ‘Welcome to (something in the place of %1%) program’. Here %1% is replaced with the first command line parameter. If you have a second and third one also, you can access it in the program by %2%, %3%, …. so on.

Output
C:Usersuser1Desktop>new HelloWorld!!
Welcome to HelloWorld!! program
Press any key to continue . . .

Color Command

This command is used to set the color for the foreground as well as background of the command prompt window. The color is specified by the hexadecimal color codes. See the below image to list some color codes.

Color Command

The foreground color is nothing but the text color that is set as default 7 and the background color is set to a default of 0.

Syntax


Program #2: To change the foreground and background of the prompt window


Output

Cls Command

Cls is the command that is used to clear the contents on the screen. There are many situations where you will be interacting with the user, displaying him a list of options and getting inputs and printing status dialogues. In such cases, you may eventually need to clear the contents on the screen. You can use this command to wipe of the screen in the current windows and start with a fresh window.

Syntax

Title Command

When you double click on the batch file and run it, you may have observed that the name on the title bar is path of the cmd.exe program located in the system32 folder of your windows directory. If you want to change the title to your desired title, this command is utilized. Title command sets the title to the current window.

Syntax


Program # 3: To illustrate title and cls commands


The output for this program must be shown in two windows as the screen wiped after executing the first echo command.

Observe that the title of the window has been changed to the text which we have given in the program. Now immediately after I press any key, ‘cls’ command is executed and the text in the prompt is wiped off.

Try it out!
Develop at least two versions of programs using the color, title, cls commands along with some conditional commands.

Leave a Reply

Your email address will not be published. Required fields are marked *