Basic Commands : Batch File Programming

Google+ Pinterest LinkedIn Tumblr +

Here I am going to explain few basic and often used commands used for constructing a simple

batch program. Before getting into the commands, there are few thing that I need to explain in detail, and

they were ‘sub-commands’, ‘switches’ and ‘parameters’.

Sub-commands:

Sub-commands are nothing but the supportive commands that are used along with the main

commands to narrow down the result that we are looking for. For example, I want to view how many user

accounts are there created in my computer, and this can be done using the “net” command, as below,

As you can see in the above screenshot, ‘net’ is the main command, where as ‘user’ is the sub-command

used for narrowing down the result that we want. A main command can have any number of subcommands

and that too depends upon the usage. Once the command gets executed, its displaying all the

available user accounts in my computer.

Switches:

Say, for instance i am going to create a new user account in my computer by making use of the

“net” command, and the user account that I wish to create is “technocrawl” with password “P4$$w0rd”

and this can be done using the following command,

16

As you can see in the above screenshot, ‘switch’ is used again to narrow down the operation of the

command that being performed, and most often switches are prefixed with as backward slash ‘/’ or with

an hyphen ‘-‘.

The above command have created a new user account named “technocrawl” with the password

“P4$$w0rd”.

Parameters:

‘Parameters’ can also be referred as ‘command line arguments’ and are nothing but the input

supplied to the program by the user while the program is running, and depending upon the parameter the

program will proceed the further operation.

Copy the below given code into a notepad and save it as ‘welcome.bat’. Goto command prompt

and run the program by using its name “welcome.bat” (Make sure that the ‘welcome.bat’ exists in the

directory where you want to run).

@echo off

cd\

echo Welcome %1%

pause

Output:

17

Where, ‘welcome’ is the batch file name and its followed by the parameter, here the parameter is

“Cybercrawler”.

Note: You can specify ‘n’ number of parameters for a batch file. Each parameter can be accessed by

using the “%number%” format, where you have to replace the ‘number’ with 1 to access the first

parameter value, and ‘2’ for accessing the second parameter value and viceversa. Incase if I want to

access the file name then it can be access by using %0%, and for accessing the fifth parametes %5% and

so on.

‘Help’ is the command that is used to display the available internal commands supported by

windows, so that you can type ‘help’ to know the internal commands available on your computer. Each

command has its own sub-commands and switches, and to find out the usage of each command in detail,

then you may use the ‘/?’ (without quotes) followed by the command, for example, if I want to know

what are the available sub-commands and switches for the ‘net’ command, then I can use the ‘net /?’

command to get more details.

Rem:

The ‘rem’ command is used for commenting the source code, so whatever that follows the ‘rem’

was ignored. The ‘rem’ command is often used for commenting large batch programs for easy

identification incase of updating of modifications.

@echo off

Rem Program for printing hello world.

Echo Hello World.

Pause

In the above example, the ‘rem’ command is used for commenting the purpose of the program, but its not

necessary for this too simple code.

18

Echo:

As said earlier ‘echo’ command is just like ‘printf’ statement in C programming, this is used to

display the text that follows the command on the output screen. Echo command when used alone will

display the state, whether it’s turned ON or OFF. By default the echo is turned ON, but it’s always

recommended for batch programmers to turn OFF the echo, so that it won’t display the prompts like

(C:\>) and so on.

You can turn OFF the echo command by using the command “echo off”, and to turn it ON, you can

replace the OFF with ON in the above command.

Color:

Share.

About Author

Leave A Reply