This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-11-22 20:45:25

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Batch/MS-DOS - A tutorial.

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:
http://download.oracle.com/docs/html/A88829_01/prompt.gif
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.

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

Code:

@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:

Code:

if "%variable%"=="Hello world" (
    echo Hello, user!
)

if "__"=="__"( )else( )
An if... else statement. This is how it would look in a file:

Code:

if "%answer%"=="scorpion" (
    echo You got it right!
) else (
    echo Try again!
)

You can also nest ifs and elses, like so:

Code:

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)


http://i46.tinypic.com/dw7zft.png

Offline

 

#2 2010-11-23 07:23:21

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

Good

Offline

 

#3 2010-11-23 07:26:52

shugocharafan88
Scratcher
Registered: 2009-10-07
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

See, scratch is the ONLY programming language I can understand.


http://29.media.tumblr.com/tumblr_m37925cD311qbdrz4o1_r2_500.gif I'm back. Here's a Kento/Shori kiss.

Offline

 

#4 2010-11-23 08:36:57

fire219
Scratcher
Registered: 2008-02-07
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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


http://bluetetrarpg.x10.mx/usercard/img.php?name=fire219

Offline

 

#5 2010-11-23 10:05:37

MasterOfDeception
Scratcher
Registered: 2009-12-29
Posts: 100+

Re: Batch/MS-DOS - A tutorial.

Pwn guide. I have never really understood batch programing, so PLEASE keep coming out with these.


"My Language Arts teacher beat Chuck Norris up." -12three

Offline

 

#6 2010-11-26 13:37:13

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

Chapters 0, 2 , and 3 are out!


http://i46.tinypic.com/dw7zft.png

Offline

 

#7 2010-11-26 13:49:06

throughthefire
Scratcher
Registered: 2009-07-09
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

I always wanted to learn batch!
Have you watched those youtube videos where people make fake viruses and pranks with batch?


Back. For now. Maybe.

Offline

 

#8 2010-11-26 14:08:28

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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?


http://i46.tinypic.com/dw7zft.png

Offline

 

#9 2010-11-26 14:21:01

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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 
pause

I don't get it, because it doesn't even pause at the end.

Last edited by Harakou (2010-11-26 14:28:02)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#10 2010-11-26 14:25:35

meowmeow55
Scratcher
Registered: 2008-12-24
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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%

Yawn.

Offline

 

#11 2010-11-26 14:28:27

Scratchthatguys
Scratcher
Registered: 2010-07-16
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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

 

#12 2010-11-26 14:35:16

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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 
pause

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

Errors:
•No quotes in the  set /p yon=Yes or No? (y/n):
•echo isn't a command, echo. is.
•No ending )


http://i46.tinypic.com/dw7zft.png

Offline

 

#13 2010-11-26 14:50:55

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Batch/MS-DOS - A tutorial.

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 
pause

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

Errors:
•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)


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

Board footer