Command Prompt Lesson 2
Welcome to Command Prompt Lesson 2! Today you will learn about how to create placeholders in your batch file. There are many ways that you can integrate these placeholders into many batch file projects, and I will start by teaching you the basics. In future lessons you can expect to see how to use variables which then will be used to create a batch file quiz! Lets get started.
Building From Last Lesson
@echo off
color a
echo Hello!
pause>nul
This is the batch file that I left you with last time. It simply prints "Hello!" on the command line.
Setting the Placeholder
@echo off
:first
color a
echo Hello! #1
pause>nul
:second
color b
echo Hello! #2
pause>nul
As you can see, there are multiple changes I made to the file.
Changes:
- Added ":first" to the 2nd line
- Added "#1" to "Hello!" on 4th line
- Added ":second:" on 6th line
- Added "color b" on 7th line
- Added "echo Hello! #2" on 8th line
- Added "pause>nul" on the 9th and final line.
Explanation for Each Change
@echo off
:first
color a
echo Hello! #1
pause>nul
:second
color b
echo Hello! #2
pause>nul
":first"
I added ":first" to the file. This is the first placeholder in the file.
":second"
This is the second placeholder in the file.
Other
- Added #1 to the first Hello! and #2 to the second one to keep track which is which.
- Added a color change to the second Hello! to make the change more visible.
- Added pause>nul for reasons explained last lesson.
The Goto Command
@echo off
:first
color a
echo Hello! #1
pause>nul
:second
color b
echo Hello! #2
pause>nul
goto first
"goto first"
The "goto" command pronounced "Go To" can be upper case or lower case. It can be gOto if you would like. This applies to all batch file commands. Though, when you type "help" into the command prompt it displays all commands in full caps. I personally put all commands and placeholders in lower case which makes mistakes less frequent. I HIGHLY advise putting placeholders and commands lower case. What it actually does is it goes to the ":first" placeholder.
Execution
@echo off
:first
color a
echo Hello! #1
pause>nul
:second
color b
echo Hello! #2
pause>nul
goto first
When you execute this program, you can see the file go to ":first" and then travel down the file to ":second" without instruction. This is because batch files read the text or program from top to bottom, executing what ever command it gets first. So without you telling it to go to ":second" it has nothing else to do so it just travels downwards.
Additions
To clean up the look of this file, try adding "cls" after each placeholder. This clears the screen to only display what is after the "cls". This will display either "Hello! #1" or "Hello #2" in their colors.
Challenge
To understand placeholders better try the following.
- Make a file with 3 placeholders and make the "goto" go to the second placeholder.
- Make a file print the same message but in different colors each placeholder.
- Make a file only clear the screen on the third palceholder, then go back to the first placeholder.
I hope you enjoyed this lesson on batch files! Please follow and favorite for more!
-Batch4Life