Introduction: Command Prompt Lesson 2

Picture of 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.

Step 1: Building From Last Lesson

Picture of 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.

Step 2: Setting the Placeholder

Picture of 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:

  1. Added ":first" to the 2nd line
  2. Added "#1" to "Hello!" on 4th line
  3. Added ":second:" on 6th line
  4. Added "color b" on 7th line
  5. Added "echo Hello! #2" on 8th line
  6. Added "pause>nul" on the 9th and final line.

Step 3: Explanation for Each Change

Picture of 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.

Step 4: The Goto Command

Picture of 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.

Step 5: Execution

Picture of 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.

Step 6: Additions

Picture of 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.

Step 7: Challenge

Picture of 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

Comments

cacj131 (author)2015-06-14

nice one I cant wait for more! :)

Batch4Life (author)2015-06-14

If you have any questions on how to make a certain batch file or want to propose an idea for a future lesson, please leave a comment below or message me privatly.

Batch4Life (author)2015-06-13

It really means a lot when you leave a comment and favorite. Also follow me for more like this!

About This Instructable

421views

7favorites

License:

More by Batch4Life:
Add instructable to: