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)
Example:
@echo off
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!!...).
COLORS!
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 off
color 0f
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
Keep Readi
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)
Parimeters:
/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.
18 Comments
8 years ago on Introduction
Great Post! I remember all the fun i used to have writing batch menus. :D
Reply 1 year ago
?? What is a batch menu and why would you need one?
Reply 1 year ago
*8 years later*
Reply 11 months ago
Dead chat gone to new level.
9 years ago on Introduction
Very useful, thanks for posting!
Reply 1 year ago
(edit) nvm
8 years ago on Introduction
I made something like this.
If %you%==want to check it out (
goto
https://www.instructables.com/id/Basic-Batch-comman...
)
Reply 1 year ago
@echo off
echo you forgot to add "@echo off"
echo nice joke though :)
pause> nul
7 years ago
Please, can you help me with this? : https://www.instructables.com/answers/BAATCH-code-Math-Problem/
Reply 1 year ago
link doesn't work anymore.
Tip 1 year ago on Step 1
You can make pictures using batch. you just need to be smart at how to make text pictures.
5 years ago
No offense, but this is a very substandard Instructable! First off, you didn't provide pictures or screenshots of the process. Furthermore, you didn't explain what program you were coding in or where to get it! Lastly, you didn't elucidate on how to run a code after it is written! Otherwise, this Instructables could have been ok, however you never made a part 2 and this didn't explain very much!
Reply 5 years ago
first of all, use notepad for batch coding. To save it change it to all files and type (name).bat. Then you just need to open the batch file that you saved and it will run
Reply 5 years ago
Yes, I know that but others that are new to batch might not know this, which is why it should have been included in your Instructable. Also part 2 also shouldn't take more than 4 years to make.
Reply 3 years ago
somebody trying to code batch would need to know these things. if you don't and you try stuff like this, that makes no sense
Reply 3 years ago
(nvm)what do i use to code batch im confused
6 years ago
stuff that
7 years ago
never made a part 2