Batch. A programming language that is simple and practical, and built directly for Windows. It can access computers over a network, or just make a fun, simple game.
Batch Tutorial by Sep
Chapter 0: Before we start.
Before we start, you should learn a little about batch, or MS-DOS.
When you write a file, all output will come up in a Command Prompt window, like this:
As you can see, it's not very good for full scale games, but it is still very useful.
Batch commands are easily written in any notebook software, like Notepad.
Batch is written line-by line, every line is a new command.
Chapter 1: The basics.
Commands to learn:
echo
Echo is probably the command you will use the most. It will print (write) the letters after it on the prompt window. At the beginning of a program,
you will most always put "@echo off" at the beginning of your program.
pause
Another great command that will wait for the user to press a key to continue. You can also use "pause>null" so the command doesn't display a message.
First Project!
Open up notepad, and paste or type this code.
@echo off echo This is the first line. echo. echo The above line should be blank. pause
Now, hit save, an d save it as "batch.bat" without the quotes.
Now, let's look at the code.
@echo off This stops the program from putting the file location before each line. Weird, huh?
echo This is the first line. This should print a line with the text "This is the first line."
echo. This works line the [Enter] or [Return] key. Don't put just "echo"
echo The line above should be blank. This should print a line with the text "The line above should be blank."
pause This should ask for the user to input a key, then the file should close.
Chapter 2: Variables and Linking
Commands to learn:
set _____ =
Set is used to create or set the value of a variable.You cannot make a good application without set.
%_____%
%% is not a command, but the symbol of a variable being called. If you set a variable, v, to 7, and echo %v%, the result will be "7".
+, -, /, *, and ^.
+,-,/,*, and ^ are all operators, like on scratch, that you can use to find results. You can add two numbers or variables this way. When using math, put /a before the variable name in set.
(string linking)
You can easily link strings together by just putting a space. If you set the variable 'name' to SeptimusHeap, and put this code, "echo: My name is %name%" the result would be "My name is SeptimusHeap".
Name & Age
(Yeah, this sorta stinks...)
@echo off set name=YOUR NAME set age=AGE echo Hello, %name%. You're %age% years old, right? set /a ageinten=%age%+10 echo Just think, in 10 years, you'll be %ageinten%. pause>null
Save it as a .bat file, and run it.
Now, let's explain the code.
@echo off This stops the program from putting the file location before each line.
set name=YOUR NAME This sets the name variable to whatever you put.
set age=AGE This sets the age variable to whatever you put.
echo Hello, %name%. You're %age% years old, right? This says hello, and says your name.
set /a ageinten=%age%+10 This figures out your age in 10 years.
echo Just think, in 10 years, you'll be %ageinten%. This shows it.
pause>null This pauses without a message.
Chapter 3: If... Else Statements
Commands to learn:
if "__"=="__"( )
The basic if command. I know. Batch is wacky. This is how it would look:
if "%variable%"=="Hello world" (
echo Hello, user!
)if "__"=="__"( )else( )
An if... else statement. This is how it would look in a file:
if "%answer%"=="scorpion" (
echo You got it right!
) else (
echo Try again!
)You can also nest ifs and elses, like so:
if "%answer%"=="bomb" (
echo You got it right!
) else (
if "%answer%"=="chuck norris" (
echo O_O
)
)Sorry, no program yet!
Last edited by SeptimusHeap (2010-11-26 13:36:46)
Offline
See, scratch is the ONLY programming language I can understand.
I'm back. Here's a Kento/Shori kiss.Offline
This is very good. Batch really is very easy, but coming from Scratch, it may look like Assembly to some people.
@shugo What don't you understand? I will try to explain it to you, maybe a little better than SH did.
Offline
Pwn guide. I have never really understood batch programing, so PLEASE keep coming out with these.
Offline
I always wanted to learn batch!
Have you watched those youtube videos where people make fake viruses and pranks with batch?
Offline
throughthefire wrote:
I always wanted to learn batch!
Have you watched those youtube videos where people make fake viruses and pranks with batch?
Yeah. So, how do you like the tutorial?
Offline
I never understood that the variable in the if command had to be surrounded by quotes... that makes no sense either. Thanks, though!
Edit: Ok, I still can't get any usage of the if command to work. What am I doing wrong?
@echo off
set /p yon="Yes or No? (y/n): "
if "%yon%"=="y"(
echo ok
) else (
echo
pauseI don't get it, because it doesn't even pause at the end.
Last edited by Harakou (2010-11-26 14:28:02)
Offline
Harakou wrote:
I never understood that the variable in the if command had to be surrounded by quotes... that makes no sense either.
It's because you can follow it with a command without using parentheses, like this:
if "%var%"=="hello" echo %var%
Offline
meowmeow55 wrote:
Harakou wrote:
I never understood that the variable in the if command had to be surrounded by quotes... that makes no sense either.
It's because you can follow it with a command without using parentheses, like this:
Code:
if "%var%"=="hello" echo %var%
That's what I do, but I try to do it with the else command and it shuts down.
Offline
Harakou wrote:
I never understood that the variable in the if command had to be surrounded by quotes... that makes no sense either. Thanks, though!
Edit: Ok, I still can't get any usage of the if command to work. What am I doing wrong?Code:
@echo off set /p yon="Yes or No? (y/n):" if "%yon%"=="y"( echo ok ) else ( echo pauseI don't get it, because it doesn't even pause at the end.
@echo off
set /p yon=Yes or No? (y/n):
if "%yon%"=="y" (
echo ok
) else (
echo.
)
pauseErrors:
•No quotes in the set /p yon=Yes or No? (y/n):
•echo isn't a command, echo. is.
•No ending )
Offline
SeptimusHeap wrote:
Harakou wrote:
I never understood that the variable in the if command had to be surrounded by quotes... that makes no sense either. Thanks, though!
Edit: Ok, I still can't get any usage of the if command to work. What am I doing wrong?Code:
@echo off set /p yon="Yes or No? (y/n):" if "%yon%"=="y"( echo ok ) else ( echo pauseI don't get it, because it doesn't even pause at the end.
Code:
@echo off set /p yon=Yes or No? (y/n): if "%yon%"=="y" ( echo ok ) else ( echo. ) pauseErrors:
•No quotes in the set /p yon=Yes or No? (y/n):
•echo isn't a command, echo. is.
•No ending )
Whoops, the first two were just copy/paste errors. Thanks for the last one; I can't believe I missed that. It still doesn't work though.
Wait, never mind. I was missing a space before the parenthesis. Man I hate languages with uber-specific syntax.
Last edited by Harakou (2010-11-26 14:54:16)
Offline