Jump to content

  •  

* * * * *

Basics to "Batch" Coding


Hello, today you will be learning how to code in the .bat file extension by simply using notepad. You can call this type of coding "Batch". Batch can be used to create simple tasks in the console window. This tutorial will teach the basics to batch coding, and the necessities to code more complex programs.

How to Create a Batch file:

  • You can do this by simply opening Notepad, or Notepad ++ if you have it.
  • Create a new file and name it anything, for example "Firstcode.txt"
  • When you compile the code and want to test it, RENAME it to "Firstcode.bat"
  • Notice the .bat which is the file extension for running Batch programs.
  • If you want to edit your code, rename it back into the .txt format and repeat the process to test it.
  • Now that you've got that settled, it's time to begin the code!

This is an example of a simple Batch code...



@echo off
echo Hello world
pause


The Command echo off is what you would use to start the code.

echo - This is the command you would use to display text.

"Hello world" - This is a string of text that is shown by the echo function.

pause - This stops the program from shutting down after the code has been executed. If you do not include this, after you click on the program, it will shutdown in probably, a split second. To see for yourself, run the code without the "pause" feature in there.

Now that you know how to display text in Batch, it's time for you to learn even more!

In Batch, you can also create loops! If you know any programming language, or have learned one before, then you probably know what a loop function does. Loop enables the program to jump to a mark and repeat the process as many times as you want.

In this section, I will show you how to create an infinite loop.


@echo off
:NAMEOFLOOP
echo This loop will
echo Repeat itself
echo Over and over!
GOTO :NAMEOFLOOP


Now then, this is yet another simple feature of coding in Batch.

When you run the program, you will notice that the message does indeed repeat itself over and over.

That is called a loop, but in this case, an infinite loop!

Loops can be dandy when creating complex codes, and not all loops have to be infinite. For instance, if you are making a code and you want the user to return to a menu, you would create the menu in a loop.

To create a loop you would do;

:LOOP // You can name it anything as long as you include the " : " operator when defining and using the loop

In this code, we did not include the "pause" command because there was no need. If we put the pause command at the end, the code will ignore it and simply jump back to the loop.

More Batch Commands:

title - By inserting this command, you can give the program a title. Normally, you would do this at the start of the code after @echo off.

Example:


title Hello world


start - By using this command, you can open programs and files. I don't think this would work if you use
spaces, but you can also run web pages!

Example:


start http://computertalkforum.com



start flower.png


By now, you should have a general idea of how Batch files work, and if you like it, you can research even further and mess with the codes. This tutorial is designed for someone with no experience programming whatsoever! Batch file is not used to create powerful, or complex programs. But it's a neat little programming language which you can pick up and work with different commands.


1 Comments

Thanks for the basic introduction!