Batch File Coding: the beginnig

Status
Not open for further replies.

Abhishek Dwivedi

TechFreakiez.com
I was a bit confused on where to post this Article but as its just an intro and not programming so i think its fine to post it in the TUT SECTION...If i am wrong then MODs are requested to move this thread...:)


BATCH FILE CODING

Batch files are really important when it comes to execute a set of DOS commands everytime. 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.

The First Batch Code:
To create a batch file you do not need any separate kind of editor, Notepad will do just fine but if you wish to code a batch file in DOS mode then type EDIT on command prompt. This will open a blue screen very similar to the Notepad.
Now, open Notepad and type:

Echo This is my first batch file

And save this as name.bat . Remember to name the file as .bat as this marks the file as batch file.
When we execute this file the DOS prompt will open up and close immediately.
Well the file executed just fine but the DOS prompt will not wait for you to view the output.
Now let’s move into different batch file commands which are really useful:



ECHO: This is the printing command of DOS. The basic function of this command is to give some printed output on the screen. The main use of the echo command comes in use when we do not wish to view any output. 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 is little command to avoid the display of the path of the directory:
Echo off

Let’s see one more example of a simple batch file:

Cd abhishek
Del *.txt
When we execute this batch file, the output comes like this:

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

Now if we add the ECHO OFF command:

Echo off
Cd abhishek
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 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.

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:

a)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:\abhi\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.
b)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.
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
gud tut
more list of dos commands here
*en.wikipedia.org/wiki/List_of_DOS_commands

also u can use
Code:
@ command line
to hide that particular command line

and list of some common batch file commands here
*www.cs.ntu.edu.au/homepages/bea/home/subjects/ith305/description.html
 
Last edited:
OP
Abhishek Dwivedi

Abhishek Dwivedi

TechFreakiez.com
thx guys...:)

@fun2sh: thx for da link buddy :D
@vaibhavtek: yes, this is a beginners guide....dats y its names "The beginning":)
 

Yavin

Yalam
Gr8 work Abhishek! Plz continue your tutorial to more advanced level. I like batch scripting and it is useful too. I am waiting for more batch tutorial from u. I think everyone should have an understanding of batch.
 
OP
Abhishek Dwivedi

Abhishek Dwivedi

TechFreakiez.com
thx yavin...i'll soon be updating dis thread with PARAMETERS and a few more batch commands...

thx yavin...i'll soon be updating dis thread with PARAMETERS and a few more batch commands...
 
Last edited:
Status
Not open for further replies.
Top Bottom