For those who are unfamiliar with batch files, I am starting this series to allow you to become better acquainted. A batch files is simply a collection of commands that are executed in a certain order that you specify. Batch files can reference and execute other batch files and can even have argument and variables which make them a great entry level coding experiment. Even if other ways exists to accomplish a task, a batch file can often be the simplest. Batch files are fun to create and always have a level of experimentation and learning about them which make the process fun.
If you have ever used MS-DOS or the “command prompt” of windows, then you shall have no problem with batch file basics. Let us begin with the editor itself. To edit and create a batch file we will use one of two methods. One method is to just open “notepad” and the other is to open a command prompt editing window. Let’s look at the simplest methodology to create our very first batch file.
To start creating a batch file:
- Open “notepad”
- Type the following:
echo Hello this is a test batch file that we are creating
START /MAX NOTEPAD
- Click “File”
- Click “Save As”
- In the File Name box, make sure that your name ends in “.bat”, “testing.bat” would work for instance
- In the “File Type” drop-down box, confirm that you select “all types” or the similar option
Now that you have just created your first batch file, let us examine the different parts.
- @echo off makes sure that only the information we WANT displayed will scroll across the command window
- echo will read off whatever words are following the command
- pause forces the user to hit “any key” before the next command will process, this allows time to read our echo
- START will start-up the specified program. In this case it is notepad. Some programs require the full file path to start
- I added /max so that the program will start in it’s maximized mode
- exit will exit out of the command prompt window so that it is not left up unnecessarily
To run this batch file, simply find the saved file and double click. To edit the file again, right click and click “edit” in the pop-up menu.
This is your introduction to batch files. Simple, Fun, and an elegant solution to many issues or needs that a user may have.
We will cover more commands and uses for batch files in the next post of this series. To continue playing around with what we have already learned, try starting OTHER programs such as calculator, firefox, paint and others. Hint: Some files need the FULL file path and some need their “.exe” extension
Until next time!