Helping people with computers... one answer at a time.
Batch files are often used to group commands, or make shortcuts to more complex commands. We'll touch on the basics of just what batch files are.
I've seen you mention a "batch file" a couple of times in articles. What is that? What's it good for?
A batch file, or command file, is a very simple text file that contains a list of programs and commands that Windows can then execute when you "run" the batch file.
The "problem" is that batch files date back to the days of MS-DOS, and are designed to contain commands that you would run within a Windows Command prompt.
I'll show you what I mean with a simple example.
Fire up Notepad (Windows Key + R, then "notepad", and press "OK"). Type in a series of commands that you want to run. Here I'll use it to run a single command which will show a simple file listing, ordered by the time files were modified.
"Dir" is the command prompt command to display a "directory", the list of files in a folder, and "/O:-D" is the option to that command to Order the results by the date/time the files were last modified, with the most recent at the top of the list.
Now, save that file as a ".cmd" file. Let's call it "tdir.cmd":
Pay attention to the full path to the folder that the file was saved in as well (or select a folder of your choice). I saved my file into a folder "t", at the root of my C: drive, or "C:\t".
Now, file up an instance of Windows Command Prompt (Windows Key + R, then "cmd", and press "OK").
Enter "CD " followed by the path to the folder into which you saved the file.
Now enter "tdir" without the ".cmd", followed by Enter:
You can see that after typing in tdir it automatically ran the command listed in the file and listed the files in that folder in reverse chronological order.
So far this has been pretty simple, but now that you've seen how to create and use a batch file, we're going to cover three concepts that give us just a peek at the power: multiple commands, parameters, and arbitrary programs.
Batch files get their name because you can "batch up" multiple commands into a single file where they are executed in the sequence listed. There's no limit to how many commands you can include in a batch file.
Let's take our existing batch file:
And make it less sensitive to where it might be:
Now if you save and run that batch file, you'll see that the current directory has been changed to C:\Windows, and that the directory listing is of the files in that folder:
As I said, simply creating and running a batch file with a list of commands you want to have executed will execute them in the order that they're listed.
So far our little batch file isn't really all that useful, because it shows the listing for only one specific folder. We wouldn't want to create a batch file for each possible folder, so instead we use parameters. We modify tdir.cmd to remove the "cd" line, because it's no longer necessary, and change the "dir" line to this:
That "%*" is a parameter substitution that is replaced when the batch file is run by whatever follows invocation of the batch file. So, for example, we can now run:
And the batch file will run:
This makes the batch file significantly more useful, as you can now specify the folder you want to have listed, or any additional parameters to "dir". Or you can specify nothing at all, and it will operate on the current directory.
So the question comes up: what can you put in a batch file?
Well, commands like CD and DIR are actually commands internal to the Windows Command Prompt, but in reality any ".exe" file (among others) will do, as long as it can be found. So, for example, this batch file:
Would start Microsoft Word, and then after Word completes and exits, Microsoft Excel (assuming that they're installed to the path shown in this example).
You may not have to specify the full path to many commands. When you run programs in the Windows Command Shell it will search for them in something called the "PATH". This normally includes C:\Windows as well as C:\Windows\System32 so any ".exe" you find there can be run in a batch file without any qualification:
This, in a batch file, would run notepad (from C:\windows) and open or create the file "notes.txt" on my desktop.
Similarly, placing your batch file into one of the directories on the path means it, too, will be available regardless of what directory may be current at the time you run it. Batch files can, in many ways, be used very much like the other programs on your machine.
I've only scratched the surface of what can be done in batch files. There are programming constructs like loops and conditionals, there are additional ways to manipulate parameters, and ways to interact with your computer's environment. There are even ways to alter that Word/Excel example above so that rather starting one after the other, they're started nearly simultaneously.
There's too much more than I can get into here, but I wanted to give you a brief overview of what they are and how they can be used in some simple circumstances.
If you're interested in pursuing batch files in more detail, a quick Google search on "batch files" returns many helpful resources.