Introduction: How to Code in Batch
Hello, I'm Mclover152 and I think i have some good batch coding experience, (Not To Brag). So I'd like to show you all how to code in batch! I will be covering the basic things. If your a fast learner and want to learn some of the hard stuff, your in luck! Soon I'll be making an instructable about advanced batch coding! Anyway let's get started!
-==/[INTRO TO BATCH]\==-
So if you have no idea what batch is, here's an intro!
Batch is a command scripting language that has the extenctions: *.bat, *.com, *.cmd, *.nt.
Batch was made by windows. It uses all the commands listed in cmd.exe, which is a default program on your computer. (If you have Linux or Apple, you might as well not pay attention to this instructable, for batch files are completely unsupported on a platform other than Windows.
Batch is not the best language to make games in, (it doesn't support graphics, only text) but since its easy to learn and requires no programs or compilers, many people can create awesome text-based games.
To Start Learning, Continue Reading!
Step 1: Part 1: Displaying Text.
Our first lesson, is the easiest, displaying text on your batch file window. (The Echo Command)
Speaking of echo, the first line of code you should always put in your batch file (not if your debugging it) is @echo off. This is required because: For example, if i didn't put @echo off, and i wrote the command echo Hello World, it wouldn't just say hello world, it would display the console output. (google it). Just remember to put @echo off.
Now to display a message, put echo (Your message)
echo Hello World!
That would display: Hello World! On your screen.
Now you probably are thinking: "Really, that's boring I wanted some style, Like pictures and graphics and stuff."
Well you can't do that, but you can do the Next best thing!... (Drumroll!!...).
In your batch file you can make a background and text color. This is done by one simple command.
Example: If I wanted to make a black background, with, white text... And display Hello World:
Echo Hello World
Now your probably thinking: "What are all these color codes?"
Well the first one (0) is a the background, and the second (F) is the text color. You can get a list of these by simply opening cmd.exe and typing COLOR /?
Keep reading for more!
Step 2: Part 2: Pausing Your Code.
Now, if your the kind of person that was really curios (not a bad thing necessarily) And you tested that code, you probably saw a flash on your screen and it disappeared. Don't worry, you didn't do anything wrong. You just told the system to exit after it was done. You need to pause your code.
To pause script, use the "pause" command. When activated, it will say on the screen "Press any key to continue." And of course, when you press any key, it will go to the next line of script. If there are no more lines, it will exit. Now your probably thinking: "Well, that's pretty cool, but i want my own pause message." You can do that, just use "pause >nul" The >nul extenction can be used in almost any command that displayed a message. Then before pause >nul! put echo "Your message without the quotes".
Batch files can also use the timeout command. Here's the syntax:
timeout (Time in seconds without the ()'s)
There's also the sleep command. Here's the syntax:
sleep (Seconds again, without the ()'s)
There's also the ping command:
ping localhost -n (Seconds without the ()'s) >nul
Step 3: Part 3: Variables.
Ok, i dont know why i put this in a single step, but variables are made by doing the set command:
(ok from now on just ignore the ()'s)
set (parimeter) (Name) = (value)
/a For a numerical or technical value or Boolean, (True or False)
/p Sets a prompt, (Well get into that in the next step)
(no variable) With no variable, it will set it as text, for example:
/set Hello-World = Hi
Never put spaces in a variable!
User prompts are quite easy, just use set /p (your name) = "Ill explain this part"
Ok so you probably thinking: "Why does my prompt need a value" (i me using >> as the variable) this is because: >> What your typing.
That's what it will display. I dont know how the choice command works so search it up.
Keep reading for more!
Step 4: Conclusion
Don't worry, I'll make a part 2.
We have a be nice policy.
Please be positive and constructive.