The Ultimate Guide To Batch Scripting From Basic To Advanced (Windows Programming Language). PART 1



Introduction

Ever wanted to know everything about batch? I don't mean the basics, I mean EVERYTHING. Well, you have come to the right place! This Post will leave you stuffed with information. By the end of this, your brain will be overflowing with knowledge.

What is Batch?

Well, Batch is a scripting language. Using Batch, you write a batch file, a compiled text file containing commands that will run in order. The name "Batch" was given to this language because a batch file contains a bundle or batch of commands. Batch files were made to finish tasks in a shorter period of time. Batch files also keep your operating system working. In every windows operating system, there is a least one batch file that is crucial to the operation of Windows. 

A History Lesson

As the Windows OS evolved, so did the use of batch files. Long ago, in the early OS versions, batch files were extremely crucial. Early versions of Windows had batch files that actually made the OS start up properly. Over time, batch files became less useful. Windows could sustain most of its frame without the help of batch files. But in a couple of years, batch files became a source of entertainment. How? Well, some people saw the advantages to batch and its endless possibilities. Soon after that, a new era was born. People started making batch games, applications, and even found ways to create simple antiviruses with them. Batch files are still a popular success today. Without those brilliant minds, I wouldn't be writing this guide. 

Requirements

So, most of you may know that all languages have requirements. Usually some special program. For example, C++ requires an IDE and a Compiler to work. So for Batch, is it the same? No. Not at all. All you need is:

-Determination
-Notepad

That's it! The only program you need is Notepad. If you don't know yet, notepad is already on your PC. Can't find it?  Just go to Start Menu > All Programs > Accessories > Notepad or Simply Press Window key+R and Type Notepad In Run and Press Enter


Now... are you ready to write a batch file? 



Writing Batch Files

Basic Commands/Your First Batch File

Ok, are you ready to write your first batch file?  
Let's get going! First off, open up Notepad.

The first line of code we are going to learn is @echo off
Now what does this line do? Well, it stops your batch file from showing file paths. It makes your program look professional. Just make sure you have this in the first line of notepad.

So, your notepad should have this in it:

Code:
@echo off

Now, have you ever wanted to make your program say something? Well if you do, you have to use the echo command. If I wanted my program to say "Hello World!”… I would type this on the second line of notepad:

Code:
echo Hello World!

So, now your notepad should have this in it:

Code:
@echo off
echo Hello World!

Let's just say this is our program. Nothing else. If we saved it (you will learn how to later) and ran it, it would pop up then close in less than a second. 
Why?!  Well, you forgot to tell the program to Pause! What, did you honestly expect it to pause by itself?  So, how do you make it pause? Well, you just put the command pause on the next line. So your notepad should look like this now:

Code:
@echo off
echo Hello World!
pause

So what actually happens when the pause command is processed during the run? Well, your batch file will pause and a message will appear. Your batch file will say "Press any key to Continue." Then when you press a key, it will move on to the next line of code. If there is no code left, the batch file will close. This may be a bit confusing, but let's run it and you will understand this.

So how do I run my program? Well, first you have to save it. Wait. I know what you’re thinking. You just going to go save it as a text document and then just open it. Yes, that is perfectly... WRONG! You don't save it as a text file. You must save it as a batch file! To do this, follow these three simple steps.

Step 1: Go to File > Save as

Step 2: Name your file but at the end of the name, insert ".bat" or your batch file will not work at all. For example, I can name it "My Program.bat" and that would work. 

Step 3: For the "Save as Type" select "All Files" not "Text Files."

This is SUPER important. If you don't do this, it will be saved as a text document.

There, you have just saved your batch file! Now it's time to run it. 
Go open the file you just saved. If it does not open, right click the file and select "Run as Administrator." Once it is open, it should look like this:





First of all, CONGRATULATIONS! 
You have just made your first batch file! Now, do you see what the Pause command did? It made the batch file say "Press any key to continue..." Then you pressed a key, and it moved on to the next line of code, which is nothing, so it closed.
  

FAQ

How do I Edit my Batch File Again?

Just right click your batch file and click "Edit." It will then open up in notepad.
Make your changes and then go to File > Save. Then close notepad and run your batch file again. All your changes should come into action.
 
My Batch File Won’t Run! HELP?!

 
Make sure you are on an administrator account. If it is still not running, right click your batch file and click "Run as Administrator." The batch file should run. If it does run but it just closes automatically, then there is an error in the source code you wrote. Go back and check that again.


Spicing up your Batch File

I quite frankly have to admit that the batch file we made a few minutes ago was boring. Some plain grey text on a black background. Exciting. 

Well, let's change that! Let's spice up this batch file of ours. First of all, let's make it look much more professional. 

Title Change

Run your batch file again and look at the title. It is C:/windows/system32/cmd.exe. Why? Well, when we run our batch file, it calls command prompt. Our batch file is run in command prompt. So cmd (command prompt) is the framework for our batch file. But how do we change this? Well, you just use the title command. Now what does the title command do? This is pretty obvious. It changes the title. So how do we use it? Let's say I wanted to change the title to "My Program." I would use this:

Code:
title My Program

It is as simple as that! But where do you put it? Well, let's just put it right after @echo off. So now your batch file source code should look like this:


Code:
@echo off
title My Program
echo Hello World!
pause


Now let's test it out. Save your source and then run the batch file again. Now look at the title. It has changed! 





Color Command


How about changing colors? Yup, you can do that as well!

But how? Well, you must use the Color command. This will be one of the most confusing things I teach you so hang in there. The color command is pretty simple. You use the word "Color" and you add to 2 characters (a letter or a digit) to change the color. Here is a list of color codes:


0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White


So the command looks like this:

Code:
Color ??


But you have to change those question marks. Do you see the first question mark in that command? That is where the background color goes. I will choose black, so look up at the chart I just gave you. Black is "0." The second question mark is the foreground/text color. I want light green. So again, I looked up at the chart above. Light green is "A." So I want a black background (0) and light green text (a). So my color command will look like this:

Code:
color 0a

There! But where do I put this? Well, put this right after the title command. So your source code should look like this:

Code:
@echo off
title My Program
color 0a
echo Hello World!
pause


Loading Effect

Ever wanted a batch file that said "Loading" then the dots increased...? Well I will show you how to do this, it's simple. Just use this:


Code:
ping localhost -n 2 >nul


Now what does that do? Well first of all, it pauses your program without the pause message ("Press any key to continue"). It does not display the pause message because of the "nul." But for how long will it pause? You choose! In this example it will only pause for 2 seconds. You can change that 2 to any amount of seconds, but do not change it to 1. Why? Well sometimes the batch file glitches and the loading effect is not shown. So how would you do the loading effect? Well, let me just write the source code and I will explain it.


Code:
@echo off
echo Loading.
ping localhost -n 2 >nul
cls
echo Loading..
ping localhost -n 2 >nul
cls
echo Loading...
ping localhost -n 2 >nul
cls


Ok so what is going on here? Let me explain. First of all, the batch file says "Loading." and then it pauses for 2 seconds (very quick seconds). After that 2 seconds, it clears the batch file and then it displays "Loading.." so it appears as if the dots or increasing. So, cls clears the screen. Cls stands for "Clear Screen." Now test it out for yourself! So your notepad should have this in it so far:


Code:
@echo off
title My Program
color 0a
echo Loading.
ping localhost -n 2 >nul
cls
echo Loading..
ping localhost -n 2 >nul
cls
echo Loading...
ping localhost -n 2 >nul
cls

Now let's run it!



It works! Now, I know some of you HATE reading tutorials, so I decided to make a video for you guys. This will cover everything we have learned so far except for the loading effect (that is pretty easy to understand when you read it).





                                       Click Here To View Part 2
                                         
                               Click Here To View Part 3

                               Click Here To View Part 4

                               Click Here To View Part 5