Forum     

Go Back   Digit Technology Discussion Forum > Community > Tutorials
Register FAQ Calendar Mark Forums Read

Tutorials This section offers tutorials and How to's on just about anything related to computers and IT. Note: All tutorials are courtesy the posters and not verified by Digit


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 26-11-2007, 06:59 PM   #1 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Arrow Batch File Coding: the beginnig


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.
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com

Last edited by Abhishek Dwivedi; 26-11-2007 at 08:06 PM.
Abhishek Dwivedi is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 26-11-2007, 07:43 PM   #2 (permalink)
Dreamweaver
 
Gigacore's Avatar
 
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
Default Re: Batch File Coding: the beginnig

nice tutorial abhi, keep it up
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore

Follow me on twitter.com/gigacore
Gigacore is offline  
Old 26-11-2007, 08:01 PM   #3 (permalink)
Fast 'N' Furious
 
topgear's Avatar
 
Join Date: Jul 2006
Location: Geek's Heaven
Posts: 11,169
Default Re: Batch File Coding: the beginnig

Nice Headstart
__________________
ToPsPeEeD = FaSt BuT StEaDy

AMD Radeon HD 6850 OverClocked to 1 Ghz !!!

Blog : http://topgeartopspeed.wordpress.com/
----------------------------------------------------
Never buy viewsonic products : http://tinyurl.com/ykwx4oa
topgear is offline  
Old 26-11-2007, 08:18 PM   #4 (permalink)
Pawned!... Beyond GODLIKE
 
fun2sh's Avatar
 
Join Date: May 2006
Location: World Of Warcraft -DOTA
Posts: 1,051
Default Re: Batch File Coding: the beginnig

gud tut
more list of dos commands here
http://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
http://www.cs.ntu.edu.au/homepages/b...scription.html
__________________
If God has indeed created Himself in His own image, then I submit to you that God is a cockroach :mrgreen:!!!!!!!!

Last edited by fun2sh; 26-11-2007 at 08:18 PM. Reason: Automerged Doublepost
fun2sh is offline  
Old 27-11-2007, 11:47 AM   #5 (permalink)
vaibhavtek
Guest
 
Posts: n/a
Default Re: Batch File Coding: the beginnig

nice 1 good for begginers.
 
Old 27-11-2007, 06:25 PM   #6 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Batch File Coding: the beginnig

thx guys...

@fun2sh: thx for da link buddy
@vaibhavtek: yes, this is a beginners guide....dats y its names "The beginning"
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 28-11-2007, 01:14 PM   #7 (permalink)
Yalam
 
Join Date: Jul 2007
Location: Chilgok, South Korea
Posts: 45
Default Re: Batch File Coding: the beginnig

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.
Yavin is offline  
Old 28-11-2007, 01:34 PM   #8 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Batch File Coding: the beginnig

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...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com

Last edited by Abhishek Dwivedi; 28-11-2007 at 01:34 PM. Reason: Automerged Doublepost
Abhishek Dwivedi is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a batch file as a service jamesbond007 QnA (read only) 5 26-10-2007 01:49 PM
Creating batch file from VB ruturaj3 QnA (read only) 2 20-06-2007 06:11 PM
batch file problem..... hansraj QnA (read only) 16 30-11-2005 02:18 AM
help needed reg. batch file. legolas QnA (read only) 4 26-03-2005 10:11 AM

 
Latest Threads
- by Who
- by Krow
- by clmlbx
- by Tech&ME
- by icebags

Advertisement




All times are GMT +5.5. The time now is 09:10 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2