Skip to main content

Batch Lesson : Beginner #1

Batch programming is the basic command line scripting language used in windows.  Is uses simple MS-DOS commands to execute a variety of tasks and functions ranging from simple sorting of strings to modifying system settings and files.  It is a necessary language to know in almost any computer related job.
In this lesson, you will be learning about a few basic commands used to display text in the command window.
As batch is a command line programming language, all of the display is shown in a Command Prompt window that looks like this:

To create a batch file, you must write all of your code into notepad, then save the file somewhere to your computer, as yourfile.bat .
The 'echo' command is used to display text in the command window.  The command is used like this:
     echo your text here
If you opened your program that only contained the echo command, it would display the text but then instantly close.  To pause the window from closing you must use the pause command.  The pause command is used to pause the code running in the window.  It will display a message that says 'press any key to continue...' .  Upon pressing a key, the system resumes that command processing.  The command is used like this:
     echo Hello
     pause
Take a moment now and try this code for yourself.  You should get something that looks like this:
This code works but it is annoying because you see all this extra text that looks like:
     'C:\Users\username\Desktop>echo Hello'
To remove this and only have the code display the results of the commands, there is a second use of the echo command.  At the beginning of your program, type 'echo off ' in the very first line.  This will tell the program not to display the commands and only display the results of the commands.
Try making the program again but this time, write 'echo off ' on the first line.
     echo off
     echo Hello
     pause
You should get something like this:
But there is still an annoying line of text that showed up before the effects of the 'echo off ' command could take place.  The solution to this is to use the 'cls' command.  The cls command automatically removes all text from the command window.  If you put it on the line after 'echo off ', then it will clear the screen immediately after the 'echo off ' command, but before the other commands so it doesn't clear them too.  Try making a code like this:
     echo off
     cls
     echo Hello world
     pause
You should get this result:
Now it only displays the text that you want it to display.
If you want to display text on different lines, then you can just use multiple echo commands on different lines.  The code should look like this:
     echo off
     cls
     echo testing testing
     echo this text is on the second line!
     pause
You can combine all these commands to make a very simple program that just displays text.  Try making something yourself.  Remember to save it with the extension .bat
     echo off
     cls
     echo Hi there
     echo abcde
     pause
     cls
     echo Bye
     pause


Comments

Popular posts from this blog

Batch Lesson : Beginner #2

In this lesson, you will learn about variables.  How to create them, modify them, and use them in your code.  Variables are used to store strings of text.  In batch, they are represented as the variable name, surrounded by percent symbols: %myVar%.  To set a variable, use the set command like this:      set abc=1234      echo %abc% This will create a variable called 'abc', that expands to the value '1234'.  Then, the program will display the text '1234' through the echo command.      set abc=12      echo %abc%      set abc=34      echo %abc% This program will display the text '12', then on a separate line, '34'. You can also set the value of a variable to nul(nothing) like this:      set var= This is different that setting the variable to 0 because this gives an output of nothing, not 0. Parameters...