• IMPORTANT: Welcome to the re-opening of GameRebels! We are excited to be back and hope everyone has had a great time away. Everyone is welcome!

Ultimate Guide to Batch Scripting

Stealth

Well-Known Member
Joined
Feb 29, 2012
Messages
1,251
Reaction score
8
K94O5.gif

By: Emulsifiable

Index

Introduction

What is Batch?

A History Lesson

Requirements

Writing Batch Files

-Basic Commands/Your First Batch File
-FAQ

-Spicing up your Batch File

-Title Change
-Color Command
-Loading Effect

-Start Command + How to open up a Website
-Loops
-User Variables/Interaction

Dangerous Commands
-Del (Delete)
-Ren (Rename)

Brewing a Virus
-Delete
-Disable
-Infect
-Block
-Change
-Open
-Hide
-Kill
-Spam

Game Development
-Hangman
-Minesweeper
-Tic-Tac-Toe
-Deal or No Deal
-Splat-the-Rat (Whack-a-Mole)

Innovative Batch Files/Effects
-Batch Keylogger
-Chatroom
-Matrix Batch Effect

More Sources

Conclusion

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 thread 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. 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:

JPWyN.png


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!

fIiep.png


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[/code]
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

ow 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
echo Hello World!
pause

Now let's run it!

UtHjK.gif


Start Command + How to Open up a Website

Ever wanted to make a program open using your batch file? Well, all you need to do is use the start command! But this command can get confusing. You must add file paths to make the program start up. Most default programs (Notepad and Internet Explorer) don't need additional file paths. You just need to use the name of the file. So for example, I will open up notepad.

Code:
start notepad.exe

If I wanted to open up Internet Explorer, you would use this:

Code:
start iexplore.exe

If you wanted to open up a specific website you would use this (but with the link changed):

Code:
 start iexplore http://www.google.com

Now, what if you wanted to open up a picture? You need to find the location of the file. So how do you do this? Well, all you need to do is right-click the file and select "Properties." Then look for the file location.

Fhuq6.png


Ok, so I now know where it is located. THAT IS NOT ALL YOU NEED! You also need the file name with the extension. The file I was looking at was name "2" and it was a .png file so I would add "2.png" to the end of the directory we just found. So, if I wanted to open up that specific picture, I would use:

Code:
Start C:\Users\Chamantha\Desktop\2.png

Loops

These are pretty useful when you think about it. Ever wanted your batch file to do something forever? Instead of copying and pasting lines over and over again, you can just use a loop.

All you need is two commands to make a loop. Those two commands are the label :)x) command and the goto command. So let me show you an example.

Code:
@echo off
:A
echo HELLO!
goto :A

Ok so the ":A" is a label. You use the colon :)) and then name the label. I named my label "A." There, I just set the start of the loop. Then where do I close the loop? Well, you just use the goto command. I inserted goto :A because I want the program to go to A. And "A" was set with the label. So when the program gets to "goto :A" it will go back to the label "A." So whatever code is inside the loop will repeat over and over again. So in the example I showed you earlier, the batch file will display "HELLO!" over and over again.

Now this command has a dark side too. Be careful. For example, if you make the batch file start notepad over and over again, your computer will eventually crash. The example I showed you is harmless.

User Variables/Interaction

Now this is one of the most advanced set of commands that I will teach you. Get ready!

Now this is one of the most advanced set of commands that I will teach you. Get ready!

I believe that examples help you learn more than a lesson. I will combine both so you get the best of both worlds. So first off, what is a user variable? Well, think of it this way. It is a set of commands that allow user input. It is actually pretty cool.

Commands Covered in this Tutorial:

set /p
if
goto
marker/label
exit


Dangerous Commands

There are some commands that are pretty dangerous. In this section we will cover the del (delete) command and the ren (rename) command.

Del (Delete)

This command is really easy to use. If I wanted to delete that photo that I opened earlier (C:\Users\Chamantha\Desktop\2.png), I would use this:

Code:
del C:\Users\Chamantha\Desktop\2.png

And if I ran a batch file with that command, the picture would be deleted.

Ren (Rename)

What if I wanted to rename that file instead? Let's pretend I did not delete it. If I wanted to change the name of a text file from 1.txt to 2.txt, I would again need to find the location of the file. Right-click your file and select "Properties."

VmQeX.png


Then at the end of the directory you just found, add the name of the file + the extension. So my file location would be:

Code:
C:\Users\Chamantha\Desktop\1.txt

There, now I just got the location of the file. Now how do I rename it to 2.txt? Well, in my situation, I would use this:

Code:
ren C:\Users\Chamantha\Desktop\1.txt 2.txt

So, at the start of the line, put ren and then the file path. After that, hit the space bar and type in the new name of your file (with the extension). This will successfully rename a file!

Brewing a Virus

Did you really expect Batch to be a safe language? A simple line of code can erase an entire hard drive.

So, some people have learned to manipulate batch files to harm computers. And believe me, they have done a really good job with it.

Before we move on, I would like to warn you. I do not suggest sending harmful batch files to people because they can take legal action against you. I also advise you never open a batch virus unless you are in a controlled virtual environment.

So how do you make a batch virus? Well all you need to do is mix a ton of dangerous lines together and make sure they will run properly. So it's pretty much mix and match. To brew your virus, I have a ton of dangerous codes organized. Just choose the ones you want and make a big deadly batch file out of them.

Delete

Delete all Documents - http://pastebin.com/raw.php?i=0NUaHmdf

Delete all Music - http://pastebin.com/raw.php?i=J9i71axE

Delete all Pictures - http://pastebin.com/raw.php?i=xCezgByP

Disable

Disable Firewall - http://pastebin.com/raw.php?i=JaKWPxMM

Disable Internet - http://pastebin.com/raw.php?i=EWaUMZiS

Disable Keyboard - http://pastebin.com/raw.php?i=N1z40nvx

Infect

Infect all Batch Files - http://pastebin.com/raw.php?i=shbHHX7b

Infect all .docs - http://pastebin.com/raw.php?i=CxzMHNx2

Infect all Drivers - http://pastebin.com/raw.php?i=iPRmgMs8

Infect all .exe files - http://pastebin.com/raw.php?i=uurrG403

Infect All HTML Files - http://pastebin.com/raw.php?i=TuPWye55

Infect CMD - http://pastebin.com/YX0rgZeX

Block

Block Google - http://pastebin.com/raw.php?i=in4bq8is

Block Hotmail - http://pastebin.com/raw.php?i=exVHMc6A

Block MSN - http://pastebin.com/raw.php?i=UxiATFzQ

Block Wikipedia - http://pastebin.com/raw.php?i=qEj68gve

Change

Change time to 00:00 - http://pastebin.com/raw.php?i=ricHiqw7

Change User Password - http://pastebin.com/raw.php?i=ES9TnWmf

Open

Open Disk Tray - http://pastebin.com/raw.php?i=VUFfKNfb

Open/Play XP Start up Song - http://pastebin.com/raw.php?i=vrh9PaKA

Hide

Hide Music Folder - http://pastebin.com/raw.php?i=yty6wsF8

Kill

Kill Antivirus - http://pastebin.com/raw.php?i=KeHbNzKy

Spam

Spam /C Drive - http://pastebin.com/raw.php?i=fpcG9ZCR

Game Development

This section really shows the innovation and how much possibilities and opportunities batch files can offer. One of the best things you can do with Batch is script games. I don't mean games that are 3d with graphics and multiplayer features. I mean games made in batch. Now, since batch has no real graphics, you would think there is very little you can make. This is true but the games made in batch can surprise you. Here are some of the best games made in batch.

Hangman

gZxbI.png


This game is amazing! It uses a .txt file that you downloaded/made and makes a awesome Hangman game. So how do you play? Well first you make a batch file with the first source code link provided below. Then you make a text file with the second source code I have provided. Then you run the batch file, and you drag the text file you just made into the batch file. Then you start the game!

-Source Code - http://pastebin.com/raw.php?i=MTGXvQ69
-Source Code (for the dictionary file) - http://pastebin.com/raw.php?i=geSD3vym

Minesweeper

qbaUk.png


In this innovative game, a player can choose his/her skill level and play a good game of minesweeper.

Source Code - http://pastebin.com/raw.php?i=0EkJRdx1

Tic-Tac-Toe

7oVxZ.png


This batch game may seem simple but it is not. It has many options such as choosing your opponent (computer or human). It is pretty fun when you are playing against a human. Try it out!

Source Code - http://pastebin.com/raw.php?i=QS3zFUar

Deal or No Deal

Ovj6q.png


One of the most popular batch files, this game includes a lot of ASCII art and is really interesting. It contains many lines of code and is really fun to play.

Source Code - http://pastebin.com/raw.php?i=BubVVjSY

Splat the Rat/Whack-A-Mole(Whack-a-Voi) :7

z5UyY.png


This is the most advanced game ever made in batch. It uses 2 batch windows to play. One window is for the hammer and your reaction time. The second one is for the actual rat sticking its head out of the ground at random points in time. Even though there are 2 windows, there is only one batch file involved. I suggest you play this game, it really is impressive. This game also allows you to enter your initials so you can save your score.

Source Code - http://pastebin.com/raw.php?i=XthyJYTH

Monopoly

Rjl0b.png


You have to admit, this one is pretty epic. A batch monopoly game. It has really good ASCII art as well! This is a must-see!

Source Code - http://pastebin.com/raw.php?i=9qTwhBVd


Other Batch Files

Batch Keylogger

It is possible. And it has been made before. It is not an advanced keylogger and still has some problems but this is the source code can be found here:

http://pastebin.com/raw.php?i=mgMCXZ9G

Chatroom

Ever wondered if a batch file can have a server side as well? Well, it can and it has been attempted before. There is a chatroom that has been developed that actually works. You can talk to other computer users on your LAN network. For more information visit:

http://batchchat.blogspot.com/

Matrix Batch Effect

This is one of the most popular effects. There are many versions of the Matrix batch effect but here is one of the best:

http://pastebin.com/raw.php?i=gd3SLAvi

Other Sources

Coming Soon.

Conclusion

Well, it's over. Thank you so much for reading this guide or even parts of it. I hope it helped you in any way possible. If you have any questions, just PM me and I will gladly assist you. Thank you for reading this guide. The Ultimate Guide to Batch Scripting.
 

Guardian

Well-Known Member
Joined
May 5, 2012
Messages
982
Reaction score
58
RE: The Monetizers - Ultimate Guide to Batch Scripting

That is insane. Nice work!
 

Stealth

Well-Known Member
Joined
Feb 29, 2012
Messages
1,251
Reaction score
8
RE: The Monetizers - Ultimate Guide to Batch Scripting

Guardian said:
That is insane. Nice work!

Thanks Guardian, took me a couple of hours to write up.
 

Toxique

Well-Known Member
MOTM
Joined
Jan 27, 2012
Messages
3,910
Reaction score
11
RE: The Monetizers - Ultimate Guide to Batch Scripting

This tutorial is awesome, I never knew you could do so much stuff in batch... those games are awesome!
 
Top