Batch file programming is a way of making a computer do things simply by creating, yes, you guessed it, a batch file. It’s a way of doing things you might ordinarily do in the command prompt, but automates some tasks, which means you don’t have to write so much code. If it sounds straightforward, that’s because it is, generally. Which is why it’s worth learning…
Batch file programming is a good place to start learning how computers work
Of course, if you already know your way around batch files, I’m sure you’ll agree it’s a good way for someone relatively experienced in software to get to know their machine a little better. If you know someone that you think would get a lot from learning batch file programming share this short guide with them!
Why would I write a batch script?
There are a number of reasons you might write batch scripts. It’s particularly useful for resolving network issues, installing a number of programs on different machines, even organizing files and folders on your computer. Imagine you have a recurring issue – with a batch file you can solve it quickly and easily wherever you are without having to write copious lines of code in the command line. Or maybe your desktop simply looks like a mess; with a little knowledge of batch file programming you can clean things up without too much effort.
How to write a batch file
Clearly, batch file programming can make your life a lot easier. Let’s take a look at the key steps to begin writing batch scripts.
Step 1: Open your text editor
Batch file programming is really about writing commands – so you’ll need your text editor open to begin. Notepad, wordpad, it doesn’t matter!
Step 2: Begin writing code
As we’ve already seen, batch file programming is really about writing commands for your computer. The code is essentially the same as what you would write in the command prompt.
Here are a few batch file commands you might want to know to get started:
- ipconfig – this presents network information like your IP and MAC address.
- start “” [website] – this opens a specified website in your browser.
- rem – this is used if you want to make a comment or remark in your code (ie. for documentation purposes)
- pause – this, as you’d expect, pauses the script so it can be read before it continues.
- echo – this command will display text in the command prompt.
- %%a – this command refers to every file in a given folder
- if – this is a conditional command
The list of batch file commands is pretty long. There are plenty of other resources with an exhaustive list of commands you can use, but a good place to begin is this page on Wikipedia.
Step 3: Save your batch file
Once you’ve written your commands in the text editor, you’ll then need to save your document as a batch file. Title it, and suffix it with the .bat extension. You’ll also need to make sure save as type is set as ‘All files’.
That’s basically it when it comes to batch file programming. Of course, there are some complex things you can do, but once you know the basics, getting into the code is where you can start to experiment.