Introduction: Batch File Guide

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.

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.

Comments

author
c0tte-sh4rk1011 made it! (author)2016-11-06

i made any text you want flash colors!

@echo off

title RAINBOW TEXT v1.3

echo Welcome, %username%, to the most useless thing ever.

echo ----------------------------------------------------------------------------------------------------------

echo type text here (to make rainbow):

set /p bleh=

:a

cls

color 0a

echo %username% says: "%bleh%"

color 0b

color 0c

color 0d

goto a

author
Mr_ MikaS made it! (author)2015-09-18

Cool, I made something like this.

If %you%==want to check it out (
goto
https://www.instructables.com/id/Basic-Batch-comman...
)

557b3d6e15be4dac6f00030b.jpegphoto.png
author
Batchcc made it! (author)2015-08-30

https://www.instructables.com/id/Full-screen-batch-matrix/

Full screen batch matrix

author
Ploopy made it! (author)2015-07-09

Cool!
I made a four function calculator:
@echo off
color 0a
goto start
:start
cls
echo Mini Calculator!
echo Choose ur first number!
set /p fnum=">"
cls
goto 2
:1
echo Choose ya operation
echo 1 for +
echo 2 for -
echo 3 for *
echo 4 for /
set /p op=">"
cls
if %op%==1 goto 4
if %op%==2 goto 5
if %op%==3 goto 6
if %op%==4 goto 7
:2
echo Choose ur second number!
set /p snum=">"
cls
goto 1
:3
echo ur result is
echo %res%
echo Do another calculation?
echo Y/N:
set /p rstart=">"
if %rstart%==Y goto start
if %rstart%==N exit

:4
set /a res = fnum + snum
goto 3

:5
set /a res = fnum - snum
goto 3

:6
set /a res = fnum * snum
goto 3

:7
set /a res = fnum / snum
goto 3

author
amei4 made it! (author)2014-03-12

launcher

@echo off

title LAUNCHER PRO

echo Hello %username%

pause

:top

cls

echo Hello %username%

echo Would you like to start

echo 1. Chrome (install)

echo 2. Internet Explorer

echo 3. Download Chrome

echo 4. Firefox (install)

echo 5. Download Firefox

echo 6. Opera (install)

echo 7. Install Opera

echo 8. Exit

set /p in=

if %in%==1 start chrome.exe

if %in%==2 start iexplore.exe

if %in%==3 start iexplore.exe https://www.google.com/intl/en/chrome/browser/

if %in%==4 start C:\Program Files\Mozilla Firefox\firefox.exe

if %in%==5 start iexplore.exe http://www.mozilla.org/en-US/firefox/new/

if %in%==6 start C:\Program Files\Opera.exe

if %in%==7 start iexplore.exe www.opera.com

if %in%==8 exit

) else (

goto loop

)

:loop

cls

echo %in% is not acceptible

echo Please write something acceptible

echo 1 2 3 4 5 6 7 8 is acceptible

pause

goto top

author
electronicz made it! (author)electronicz2014-04-09

Cool, I made something like this in the past : )

-WorldBuilder

author
mjlush made it! (author)2014-01-19

I'm really sorry to be negative about this .. but to be really really honest I would never advise anyone to put much effort into learning 'batch' programming. For one simple reason that a) its not portable (this instructable will work in MS windows and nowhere else) and more importantly b) its kind of a dead end (you start to push the limits quite quickly). I'd suggest that learning a scripting language like Python or Perl, you can do everything you can do in batch and its a gateway to much much more. The only time batch is worthwhile is when your not in a position to install anything better or your writing a smarter launcher/installer.

author
electronicz made it! (author)electronicz2014-01-19

Hi, thanks for the feedback, and you aren't being negative at all. I whole heartedly agree with you. I recommend people to look a little at Batch and then learn something like Python (that is what I have been learning). I think someone shouldn't get farther than as far as this Instructable goes, because it is just meant for moving files, deleting files, and doing different tasks on your computer. You can make small programs and little games with Batch, but something like Python is way better suited for the job. Thanks for the feedback and I agree with you.

About This Instructable

8,407views

29favorites

License:

Bio: Hi! I am learning the Python programming language, I also play some Minecraft . So, I hope you like the stuff I post, and please comment ... More »
More by electronicz:Batch File GuideSimple Long Range Minecraft CannonSony Vegas Studio Lightsaber Effect
Add instructable to: