Support Forums

Full Version: [Very usefull] Basic .bat tutorial [MUST read]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This tutorial is very basic and for my feeling pretty easy to read. If there are any errors in this tutorial please leave a comment and I will rewrite the flaws. All comments are greatly appreciated and please don't be to hard on me regarding the grammar I've used. There is no section for this so I figured it would fit best in this section if anyone thinks it should be somewhere else tell me please.
Enjoy!

Why should you start batch programming
Batch programming is a simple scripting language. It can be executed on any windows OS without any extra software. It is created in notepad which I think is very handy. It doesn't have as many functions as for example C++. But it comes in very handy if you can program in batch. For example if you want play a joke with your friend, make a computer destroying virus or want to make a handy installer.

What is batch programming
Batch programming is a programming language that is for MS-DOS. It will be excecuted in a MS-prompt. If we look up the definition of batch this will show up: n a computer, a batch job is a program that is assigned to the computer to run without further user interaction. Examples of batch jobs in a PC are a printing request or an analysis of a Web site log. In larger commercial computers or servers, batch jobs are usually initiated by a system user. Some are defined to run automatically at a certain time.
In some computer systems, batch jobs are said to run in the background and interactive programs run in the foreground. In general, interactive programs are given priority over batch programs, which run during the time intervals when the interactive programs are waiting for user requests.
The term originated with mainframe computers when punched cards were the usual form of computer input and you put a batch of cards (one batch per program) in a box in the sequence that they were to be fed into the computer by the computer operator. (Hopefully, you got the output back the next morning.)

How to create a .bat and how to edit it
The only program you need to create a batch is just simple notepad. The windows you'll get when you open notepad is the window were all your codes will be. After you put in your codes you should press "save as". Save it as anything.bat. Below the box were you can put in the file name there is a box that says: "textdocumt .txt". We need to change this to "all files". You also need to make sure you have the option "show file extensions" enabled. If you haven't you need to open "my computer". Then go to "extra" (in the menu above) --> "Options" --> "display" then tick the box "Show file extensions". When you have saved your file you need to edit it. If you click it it won't open becuase it then executes the commands. You need to press the right mouse button and then press "edit".

Your first code
The first code i'll learn you is the echo command. This command will show a message on the screen. To do this you will put in the following code:

Code:
echo i'll be a badass programmer

This will show the whole message, including the echo. We don't want that! That's why we need to put in the following code on top of the screen: @echo off. Our code will then be:

Code:
@echo off
echo i'll be a badass programmer soon

You'll notice that the window will display the message and rigt after it will close. We also don't want that. We use the pause code for that. The pause code will pause the code until you'll press a button on your keyboard. To use this we will put in the following code:

Code:
@echo off
echo i'm a badass programmer
pause

You'll see the in the window the following message: "press a button to continue". If you don't want that you can make a small change so there won't be a message. Instead of the pause command you'll need to put in "pause>0". It will just stop, the user will have to figure out thereself they need to press a key or you can say it with the echo command like this:

Code:
@echo off
echo i'm a badass programmer
echo if you are ready to continue press the button you wish
pause>0


Title and color and cls
The title is an easy command. It won't get any easier than that. If you want to get a title that says "crackhackforum.com" the code you have to use is:

Code:
title crackhackforum.com

If you want to change the color of the window, the color of the letters and of the background. You have to use the code "color" then a space and then your color code. The color code exist of two letters/numbers or one of both. The first one is the background and the second one the text. If you want a matrix look you would paste in this code:

Code:
@echo off
color 0a
title Matrix!
echo I like the matrix
pause[/color]

The colortable is:

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

Puzzle with those codes and find out which combination you like. Sometimes if you have a long code the whole prompt is full of text. If you want to erase all text in the window (not the text in your code) you need to use the code "cls". For example:

Code:
@echo off
echo This is a very long sentence and my whole screen is filled because my screen is 5cm.
cls
echo Now my screen is empty again

Remark, stop a batch and delete a file
If have a long code you may want to make a remark at some point. you can do it by using the command "rem", as showed in this example:

Code:
@echo off
echo Easy huh?
rem this is my remark no one will see

No one will see the remark you have made, but you'll see when you are editing your code. If someone sends a code to you that pops up windows all the time and you can't close it, or if the file is going to delete a file. You want to the batch, but sometimes you can't just exit by clicking. It those situations you need to press cntr + c. The prompt will then ask if you want to stop something like this: "stop this program (y/n)". Y stands for yes and N for no. So press y and then enter.
If you want to make a file that deletes a file or a whole bunch of files let's say you want to delete all files in the map "C:test". You'll need to use the "del" command. This is still a pretty simple command. If you want to delete the file you should use this:

Code:
del "C:test"

Now the map "test" will be deleted.

It took me quit a while to write so please be kind and leave a comment. Thanks
This should be in the programming section. Nonetheless, it is a good tutorial. Congratulations mate.
(05-08-2011, 09:42 AM)Telhast Wrote: [ -> ]This should be in the programming section. Nonetheless, it is a good tutorial. Congratulations mate.

There is no section in the programming section about .bat tutorials so I figured the best solution would be to put it in the windows section.