When I began learnig batch, one of the first things I did was buid a small calculator, and thoguh it was very simple, it was a greaet start to learning how to utilise variables.
So I decided to make this instructable that will show you how to:
- Use some basic batch commands
- how to build a simple caclulator
I've also included a small project of mine, which is currently at v0.8.
I might take it to v1.0, and if I do so i'll update it to this instructable.
NB : The Calculator file at the end of every step is my advanced one
Step 1: Basic Batch
Read this if your new to batch, and even if your not skim it for some usefull things you may not know.
- @echo off
> Suppresses the console from displaying what commands you type into the batch file and only displays the output data
- Echo [text]
> Displays the text on the console
- Set "var=val"
> Creates a variable named "var" with the value of "val"
- Set /p "var=Prompt"
> Prompts the user with "Prompt" and what ever they input becomes te value of "var"
- Set /a "var=Equation"
> Solves "Equation" as a maths equation and sets the value to "var"
> Refers to the variable amed "var"
- %var:~[Refer to values within "var"]%
> To learn how to use the above type "set /?" in cmd.
- if "%var%" equ "val" [command]
> Executes [command] if %var%=val (if /? for more info)
> Creates a tag called "tag" which can be later referenced
- goto tag
> Goes to the tag labelled "tag"
- Color [hex][hex]
> Sets teh current console colour to the two hexadecimal numbers (color /? for more info)
> A line starting with Rem will be completly ignored. It's used to organsise the file or help someone else understand it.
- Title Program Name
> Sets the console's title to "Program Name"
> Displays "Press any key to continue . . ." and continues upon user input
> Immeadiately exits the batch file
And thats your crash course to batch!
Of coourse, type help to learn more. Lastly this is nothing compared to some of the operatable programs built in to windows. And nothing when compared to building you own operable programs!
But this is just your first little step...
Step 2: Your Calculator!
Firstly open notepad.
In notepad type the code:
Title My Calculator!
set /p "input=Calculate: "
set /a answer=%input%
Rem This Line is a remark and will be ignored. So will the below line. Its just good practice to End all Batch Files like this
Exit /b 0
And thats it
NB: Echo. Leaves a blank line
Then Ctrl+S to save and save as "Calculator.bat" with the option "allfiles" selected (note ".txt - Text Document") Some versions of Windows will not except the latter.
Finally find and run it and have FUN!!! Modify it to make it Mind blowing!
Step 3: Try My Calculator
If I do update it I'll put ip up here again.
If you want me to make more steps (or a new instructable) Explaining how to build one like mine, Comment.
Note, when using it type "?" so you know how to use it.
Don't dedicate your life to finding cases where it breaks. Dedicate your life to:
conveying potential aspects of belief or opinon which may or may not have didactic influence over others who amy ever wish to accept so of the great potential and power of a simple automated series of tasks which take user input and resolves it to the humans standard of maths, with the awkward fact that the functions require the operator to be placed in fromt of the operand???
I'll go neck my self about. . . . . . . . . . . . Pause . . . . . . . . . . . . . Now!