Lesson 1: Introduction to Batch Files
I'm sure many of you have seen that little black box with the blinking underscore once or twice in your encounters with Windows. What many of you may not know is that by harnessing the power of batch coding, you're unlocking a massive coding language built into Windows!
Pros of Batch Coding:
- Easy to learn
- Does not need a compiler
- Simple syntax and API
- Compact
- Powerful
Cons of Batch Coding:
- Not as useful compared to languages such as C++
- Can be dangerous
- Usually causes suspicion across the internet
With that out of the way, I can continue to teach you the basics of batch coding.
Customary to all intros to coding, I will start off with the ever famous "Hello World!" app.
To begin, you can either use a batch IDE (I recommend Notepad++) or just use Notepad.
The "Echo" Command:
The first command we will learn is the "echo" command. The "echo" command prints the desired word or phrase onto the console.
We will write this into the IDE or Notepad:
Notice how "Hello World!" doesn't need quotation marks around them. This is the beauty and simplicity of batch coding.
You can now save the batch file through notepad as a .bat file or compile with the IDE.
But when you try to execute the file, the black box pops up for half a second and disappears! Why is this?
Well to put it simply, you didn't give the file anything to do after echoing "Hello World!", so it simply exited after finishing its task.
To give the user some input and control over the program, we will use the "pause" command, which will be explained in the next section.
The "Pause" Command:
The "pause" command will be used a lot during your encounter with batch. The command prints the words "Press any key to continue..." on the screen and then waits for your input.
When we add this little snipit to the code, we get the following:
Code:
echo Hello World!
pause
After compiling/saving, and after executing, we get something that looks like this:
But upon examination, we see the commands being executed also printed on the console. Why is this? This is because the commands are not being told to hide themselves from the user.
You will be taught how to hide those commands in the next section.
The "@echo" Command:
In most batch files you see, you will probably see the command "@echo off" dominating the spot of first command. This command simply tells the program to not show what commands are being executed and simply shows the output.
Note: This command is not to be confused with the "echo" command. Without the "@" symbol, the program will just simply print the word "off" on the console.
Inserting this new command, our code should look like this:
Code:
@echo off
echo Hello World!
pause
After compiling/saving, we should get something that looks like this upon execution:
There! Doesn't that look more clean and neat?
Now that you know the basics of batch coding, stay tuned for the next lesson on batch files that can be found here:
[Batch]Lesson 2: Improving Your Code