Wednesday, September 24, 2008

Batch File Programming Part-2

Now let's execute this batch file and see what results it shows. Launch command.com (DOS) and execute the batch file by typing:

C:\WINDOWS>batch_file_name
You would get the following result:
C:\WINDOWS>scandisk
And Scandisk is launched. So now the you know the basic functioning of Batch files, let' move on to Batch file commands.

The REM Command:
The most simple basic Batch file command is the REM or the Remark command. It is used extensively by programmers to insert comments into their code to make it more readable and understandable. This command ignores anything there is on that line. Anything on the line after REM is not even displayed on the screen during execution. It is normally not used in small easy to understand batch programs but is very useful in huge snippets of code with geek stuff loaded into it. So if we add Remarks to out first batch file, it will become:

REM This batch file is my first batch program which launches the fav hacking tool; Telnet

telnet

The only thing to keep in mind while using Remarks is to not go overboard and putting in too many of them into a single program as they tend to slow down the execution time of the batch commands.


ECHO: The Batch Printing Tool

The ECHO command is used for what the Print command is in other programming languages: To Display something on the screen. It can be used to tell the user what the bath file is currently doing. It is true that Batch programs display all commands it is executing but sometimes they are not enough and it is better to also insert ECHO commands which give a better description of what is presently being done. Say for example the following batch program which is full of the ECHO command deletes all files in the
c:\windows\temp directory:

ECHO This Batch File deletes all unwanted Temporary files from your system ECHO Now we go to the Windows\temp directory.

cd windows\temp

ECHO Deleting unwanted temporary files....

del *.tmp

ECHO Your System is Now Clean
Now let's see what happens when we execute the above snippet of batch code.

C:\WINDOWS>batch_file_name
C:\WINDOWS>ECHO This Batch File deletes all unwanted Temporary files from your system

C:\WINDOWS>ECHO Now we go to the Windows\temp directory.
Now we go to the Windows\temp directory.

C:\WINDOWS>cd windows\temp

Invalid directory

C:\WINDOWS>ECHO Deleting unwanted temporary files

Deleting unwanted temporary files...

C:\WINDOWS>del *.tmp

C:\WINDOWS>ECHO Your System is Now Clean

Your System is Now Clean



The above is a big mess! The problem is that DOS is displaying the executed command and also the statement within the ECHO command. To prevent DOS from displaying the command being executed, simply precede the batch file with the following command at the beginning of the file:

ECHO OFF

Once we add the above line to our Temporary files deleting Batch program , the
output becomes:

C:\WINDOWS>ECHO OFF
This Batch File deletes all unwanted Temporary files from your system
Now we go to the Windows\temp directory.

Invalid directory
Deleting unwanted temporary files...

File not found
Your System is Now Clean

Hey pretty good! But it still shows the initial ECHO OFF command. You can prevent a particular command from being shown but still be executed by preceding the command with a @ sign. So to hide even the ECHO OFF command, simple replace the first line of the batch file with @ECHO OFF

You might think that to display a blank line in the output screen you can simply type ECHO by itself, but that doesn't work. The ECHO command return whether the ECHO is ON or OFF. Say you have started your batch file with the command ECHO OFF and then in the later line give the command ECHO, then it will display ' ECHO is off ' on the screen. You can display a blank line by giving the command ECHO.(ECHO followed by a dot)Simply leaving a blank line in the code too displays a blank line in the output.

In the next post we will see some more concepts of Batch File Programming.

No comments: