Hi! Welcome to my Batch File Guide, I am relatively new to the batch language. This is not an extremely detailed guide, because I believe that it is good to learn about this much of Batch and then start learning something like Python. Also, if you have any suggestions or see something wrong with my guide, then please contact me, I like feedback. Remember, if you like my Instructable, please comment, favorite, and or SUBSCRIBE! Thank you.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Saving Your Batch File
After you write your batch program in your text editor (I use Notepad), you have to save the program as a .bat file. So you save it as your_file_name.bat, youmust have the .bat ending every time or it will save as a .txt file instead of a .bat file.
Step 2: Printing "Hello, World!" Onto the Screen
Well, your long awaited first batch file shall come at last. In it, you will print "Hello, world!" onto the screen. The function to print text onto the screen in batch is echo. Another thing to know is, in most batch files you make, the first line of code is @echo off, this makes it so the echo is hidden and it only shows the text you want to be shown. Now, put that into your code and then put echo Hello, world! on the next line. Now, if you save it as hello_world.bat and double-click on the batch file, it will open a CMD window and close it really fast. To keep it open and to let the user see the text you must use the pause function, so, on the next line type pause. Now finally, you are done, save it and double-click on the batch file icon. It should open a CMD window, print "Hello, world!" in the window, and say "Press any key to continue . . . ", after you press any key the window will close. Congratulations, you just made your first batch file!
Step 3: Making Variables in Batch
In this step you will learn how to declare variables and variable types in Batch. First you will have to know about two things: strings and integers. A string is usually letters and can be numbers. An integer can only be numbers and not letters, if you want to have a variable with only a number then you should probably make it an integer. For example, if you have two strings, the first is 1 and the second is 2, then if you try to add them together it will make something like 1+2 instead of 3. If you have the same values as integers, then if you add them it will make 3. So, enough talk and now some code, to make a string variable, do set var=testing. And to make an integer variable do set /a var=12. If you want to make a variable that gets the users input, then do set /p var=. Now lets say we want to put the var we got from the user onto the screen, then we do echo %var%. I included a small adder program that uses all the things you have learned.
Step 4: The IF Statement and Functions
The powerful IF statement is before you, examine it carefully. Imagine if we did set /p input= and we want to check if the user input is 1 and if so write something, then we do this if %input%==1 echo User input is 1, that is a basic IF statement. Now something really cool are functions that you can make yourself. Like echo is a built-in function of batch, you can make custom functions by doing :function_name and then writing your code after that. And also after all of your code is finished inside the function you have to have a white space or it will not work correctly, and to run the function just do goto :function_name and it will run the code inside of it. Now with some imagination, we can make some pretty cool programs. On a side note, a useful function is cls it clears all the text off of the CMD window and I usuaIly use it after a pause function. I included a small calculator using most of the stuff we have learned in this guide.
Step 5: Code Corner
Below are some programs that I have posted because I thought they were cool or I made them. If anyone wants text source code instead of a file, then please just PM me. And if you want your program posted on here, PM me with the text source code for your program/game and I will check it out and consider posting it on this page. Also if you find any bugs with the programs PM me. Thanks.