2.1.1. Sample DOS Batch File¶
This is how a sample DOS Batch file would look like.
1@ECHO OFF 2REM %%%%%%%%%%%%%%%%%%%%%%%% 3REM % Sample batch file 4REM %%%%%%%%%%%%%%%%%%%%%%%% 5ECHO Hello %USERNAME%! How are You?
If we run this batch file, the output would be as below:
Hello Dehlia! How are You?
Let us try to understand what each of those 5 lines mean.
- line 1:
It’s a good idea to put
@echo offin the beginning of a batch file.
This line means, do not echo the Commands that are going to be run form this file. Just execute them. And if they have outputs, show the output.
If you want to see everything step by step, then comment it out. How to comment it out? Continue reading.
- line 2-4:
Comments start with
If you wonder what REM stands for, it is for Remarks.
Remember, It is always a good idea to put comments in your scripts
- line 5:
ECHOis the echo command. It’s used to print some text.
%is used to expand a variable.
USERNAMEis a standard DOS Variables that stores the login ID of the current user.
%USERNAME%would expand to the value stored in environment variable
USERNAME. In this case, it’s
2.1.2. Sample DOS File — with echo on¶
Now, if we remove
@ECHO OFF line, the commands are shown as they are executed. Sometimes, this may be the expected behaviour. Either you are debugging the batch script, or you want to let user know all command that you are running.
If we have a sample batch file like this,
1REM % Sample batch file - with ECHO ON 2echo Hello %USERNAME%! How are You?
And if we run it, the output would be as follows:
C:\Temp>REM Sample batch file - with ECHO ON C:\Temp>echo Hello Dehlia! How are You? Hello Dehlia! How are You?
2.1.3. Sample DOS File — echo on off¶
@echo off would make everything silent.
@, we can silent some selected lines from printing. If we have a batch file like this, and we run it
1@REM % Sample batch file - with @ for echo off 2echo Hello %USERNAME%! How are You?
The output of above program would be:
C:\Temp>echo Hello Dehlia! How are You? Hello Dehlia! How are You?