Saturday, July 23, 2011

Programming A Batch File

Batch files are really important when it comes to execute a set of DOS commands every time. Basically batch file programming is nothing but the Windows version of Unix Shell Programming. In the article we’ll try to learn the basics of the batch file coding.
The Basic:
Batch file is basically a executable file which executes the definite set of commands in one go. It is important "not to name a batch file after the name of any DOS command" as if done so, the batch file will not execute.

Creating a Batch File:
To create a batch file you do not need any separate kind of editor, Batch file can be created by two methods :-
1)Notepad
2)DOS - Open command prompt and type EDIT(2k/XP) a blue window will open very similar to Notepad.


Programming Batch File:
Here i will tell you the use of very basic command i.e. ECHO.
ECHO: This is the printing command of DOS. The basic function of this command is to give some printed output on the console. ECHO command comes in handy when we do not wish to view any output. Its like hiding the command from the viewer. When we execute a Batch file, it displays all the commands which are being executed preceding the path of the directory where they are being executed but if we wish we can add this little command to avoid the display of the path of the directory:
"ECHO OFF"
Doing it practically-
Follow the above steps to create batch file and write following commands-
" Cd kausttubh
  Del *.txt "

When we execute this batch file, the output comes like this:

C:\Windows\cd kausttubh
C:\Windows\kausttubh\del*.txt
Files deleted

Now if we add the ECHO OFF command:

" Echo off
  Cd kausttubh
  Del *.txt "

The output of this code will be:
C:\Windows\ECHO OFF
Files deleted

This code will remove all the commands being executed and just show the errors and notification but still show the initial ECHO OFF command on the top. If you wish to hide this ECHO OFF command from showing then add @ECHO OFF. When you add this command, it will not even show the ECHO OFF command on the DOS prompt.

If you have used the ECHO OFF command in the beginning of the code the all the other echo commands after ECHO OFF will not work and you will get a message:
ECHO is off
You can turn on ECHO by typing ECHO ON. This will give you a message:
ECHO is on

PAUSE: This is one of the most important command when it comes to interact with the user and stop the DOS screen to view the output. Let’s see a code:

ECHO hello
Pause

When you execute this code, the result would be like this:

C:\windows\ECHO hello
Hello
Press any key to continue…

Now you must have clearly understood the use of this command and how this halts the DOS screen. Now as you can see that the code is asking you to continue or not and if you do not wish to continue, press Ctrl+C or Ctrl+break. Pressing this key will give you a message:
Terminate batch job (Y/N) _
If you enter Y then the code terminates and if you enter N, the code proceeds to the next command.

Various Commands For Writing a Batch Code

IF command: The IF conditioning command is also available in batch file coding. The batch file programming gets a bit complicated here as at this stage we start using the parameters but as we are not familiar with the parameters at the moment, we will only check a few examples of IF conditioning:

<!--[if !supportLists]-->a) <!--[endif]-->

File existence: This is my favourite as it lets you check whether a file exists or not and take appropriate action for the state.
SYNTAX: IF EXIST path command & IF NOT EXIST path command. It is also possible to check the existence of more than one file by using the IF statement again in the same line like IF EXIST path IF EXIST path command. In order to check the existence of a Directory by the following Syntax: IF [NOT] EXIST path\nul (example C:\doc\nul) command. To check the existence of a Drive we use the following syntax: IF [NOT] EXIST drivename\io.sys command , this syntax is a bit old and I have only checked it on a Windows 2000 server but not on XP or Vista.
<!--[if !supportLists]-->b) <!--[endif]-->

Compare strings: I’ll not be explaining this syntax as we need to understand the parameter before we understand this syntax.
SYNTAX: IF [NOT]stringX==stringy command.
Note:
Its really sometimes get very time taking and annoying to always migrate to the directory where you have saved your batch file and then execute it. So, inorder to avoid this migration you can create a separate directory where you can save all your batch files and add this directory in the PATH statement of the AUTOEXEC.bat file which is present in the root directory of your system. Just open the AUTOEXEC.bat in notepad and look for a line which starts with PATH and then add the path of the folder in that statement. This way you can execute the batch file from any directory by typing in the name of the batch file.

Avoiding Batch file Viruses: When you double click on a batch file, it automatically executes and this can be very dangerous sometimes. To avoid this do the following:
Open the Registry and go to HKEY_CLASSES_ROOT\batfile. Open the EditFlags binary value and change its value to 00 00 00 00. Now open Explorer and open folder option from the view menu and select File Types Tab, scroll down to the ‘MS-DOS Batch File’ item, highlight it and click Edit.
Now when you double click on any batch file, it will open it on the default text editor and hence you are safe from the batch file viruses.

1 comment:

C & C are welcome.