顯示具有 Batch Tutorials 標籤的文章。 顯示所有文章
顯示具有 Batch Tutorials 標籤的文章。 顯示所有文章

2012年6月24日 星期日

Batch Tutorial 02 - Choice

Today we are going to learn "choice" command!
It is really useful in communicating with the client.
It gives a choice of however many you want, determined by the keys you want them to press.
Default keys are Y and N.

choice /n
"/n" hides the choices that you have given, you use this if the choices are already listed.

choice /m
"/m" sends everything behind it as a message, most of the time it is placed last

choice /c
"/c" is the one that allows you to set the choices, let's say if you have "choice /n /c xyz" then there are three choices : x, y and z.

choice /t
"/t" sets the time that the client needs to chose in.

choice /d
"/d" choses the choice after it if time set in "/t" has ran out.

Errorlevel
Errorlevels are important, let's use the last example.
In x, y and z, the first choice is errorlevel 1, followed by 2 and 3.
x is errorlevel 1
y is errorlevel 2
z is errorlevel 3
Here is an example :


@echo off
choice /n /c xyz
if errorlevel 3 goto c
if errorlevel 2 goto b
if errorlevel 1 goto a
:a
ehco You took x
pause => nul
exit
:b
echo You took y
pause => nul
exit
:c
echo You took z
pause => nul
exit

The errorlevels must start from the biggest number and end with the smallest or else it will not work (I don't know the reason as well)
We will be using this command in our next tutorial, and I hope you understood!

2012年6月22日 星期五

Batch Tutorial 01 - Multiplication Table


Hey guys! 
In this tutorial we are going to help children!
Actually it's just a simple multiplication table... but let's not be demotivated!
Alright! What is always the first step?

@echo off

That's right!
Now we need three values: a, b and c
The values will represent a certain number that you set it to.
Now, you simply set numbers by using "set" command.

set a=1
set b=1

We will leave c for now.
The idea is to have the number substituted into the equation "a*b=c"
Now we need to have our system planned.
Let's just move on and I will explain every part.

:a

When you have a letter or a word after ":" it is like a check point.
With "goto" command you can move to check points.
If you read the first tutorial, the batch file runs from top to bottom.
"goto" command forces it to keep going from the check point.
Now, let's do our calculation!

set /a c=%a%*%b%

Note that when you set an unknown, you do not need "%", but when you use the unknown as a value you need it in front and behind the letter.
The "/a" behind the "set" command is to calculate, any value that needs to be calculated from other values, needs the "/a" after "set". In this case, it sets "c" to "a*b" which is 1*1=1
All these will NOT be shown to the client, to show a message to the clients, use "echo"
"echo" will show everything on that line except for the space behind it.

echo %a% x %b% = %c%

We will use "x" for now, because it looks like the multiplication symbol in the cmd.
Now we should have "1x1=1" done.
let's set b to 2

set /a b+=1

"+=" increase the value before it by the value after it, "-=" does the exact opposite.
Ok, now we can use "goto" to make it loop, but we don't want b to be anything more than 9.
We need a new check point, and we need to make sure that when b gets to 10, it goes to the check point.

if %b%==10 goto b

Ok, now let's discuss what ":b" does.
It sets the a value to one higher, and sets b to 1.
Then return it to ":a"

:b
set /a a+=1
set b=1

Just stop there before you type in the "goto" command.
Think about it, if we have "goto a", then there is a loop which multiplies a to 9 and increase it by 1
But we do not want a to be any higher than 9

if %a%==10 goto c

Let's deal with c later on, now we can have "goto a"

goto a

":a" and ":b" are both done, now let's make ":c" pause the whole thing

:c
pause => nul

The "=> nul" stops "pause" command from showing "Press any key to continue..."
Now we connect the whole thing together:

@echo off
set a=2
set b=1
:a
set /a c=%a%*%b%
echo %a% * %b% = %c%
set /a b+=1
if %b%==10 goto b
goto a
:b
set /a a+=1
set b=1
if %a%==10 goto c
goto a
:c
pause => nul















Save it as whatever name you want followed by ".bat"
This is the end of this tutorial, hope you like it!

2012年6月21日 星期四

Batch Tutorial 00

I am doing this tutorial, assuming that the readers have no knowledge about it.
Firstly, we need to be clear of what batch is.
You can make simple games, or useful files with it, and all you need is a notepad!


Goto start, run (or search), type in "cmd"


Ok, now you should have a black screen appearing.















That is a command prompt, it allows you to access your computer in a different way.
Alright, let's get to know the commands.

Type in "help"


You will see a list of commands that you can access. You do not need to know all of them.
Now open a notepad. (or any other extended version such as Notepad++)
If you have a notepad full of batch commands and save it as batch file (.bat) , the computer will then run the commands from top to bottom rapidly.
Note : You do not need cmd to be opened, it will start a cmd when it is ran.
Now you can close your command prompt if you wish to.

In your notepad, type in the first line "@echo off"


This makes a difference, not in how the file works, but how it looks.















without "@echo off" as your first line, you will have the line of your command which in my opinion, it's ugly.















The line is now removed with "@echo off"
This is the end of tutorial 00, in tutorial 01 we will start making simple batch files!