Wednesday, November 16, 2011

Batch Files: getting started part 2

The first line in a batch file often consists of this command
   @echo off
By default, a batch file will display its commands as it runs. The purpose of writing this first command is to make sure the display is turned off. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. If you wish to make the turn off command apply to the echo command as well then you type the "at" sign "@" in front of the echo command
i.e. @echo off
It doesn't really affect the batch file whether you use "@echo off" or just "echo off" . The scripts we will discuss are very brief and omitting this line won't make any great difference. However, as a matter of good practice, it will be wise to enter it in our scripts, especially if the script is going to be a long one.
If you are familiar with  the commands used in CMD, one of them that we mostly use (or i use a lot) is the "dir" command, without the quotes...It lists all the files and folder in the present directory you are on...try it now
open command prompt,  to run command prompt >>
on windows XP click run...and type cmd....OR go to accessories and click on cmd
on windows 7 just search for cmd and run it,
you will see the prompt below as your present directory if you installed to drive C: or something similar depending on your machine and where you installed your windows

C:\"your work-group e.g Users"\"your home folder">

now type in the dir command and a list of all the files and folders present there will be displayed...
the ones that have a <DIR> in front of them are folders while the rest with extensions e.g .txt .exe .bat are files...

The first batch file example i want to introduce will just
list the content of a directory on the command screen.
Open notepad
type in @echo off
Next on a new line, type in cls
Next on a new line, type in dir
Next on a new line, type in pause
Next on a new line, type in exit

save the file above as listing.bat     Make sure you do not save it as .txt but as .bat
After saving the file, to run it go to where the file was saved and double click on it, you should see the command prompt pop up and your home directory listing will show next..

  • From above, you will notice that the first line is "@echo off" ,this suppresses the prompt from displaying...
  • Also, the next line "cls"  clears the command screen of any previous text.....
  • Then we have our "dir" command to list the content of the present directory....
  • Followed by "pause" which temporarily stops the batch file execution until a button on the keyboard is pressed. When executing you will see "Press any key to continue . . ." displayed where the pause command was placed in the batch file.
  •   The last command is "exit", this just exits the command window, i.e. closes the command window 
I hope i you now know the basics of a batch file and how they are saved and executed....
next, i shall expand the above example to make it check out part III coming soon 

No comments:

Post a Comment