What's a batch file?
Hi everyone. Leo Notenboom.
Here for askleo.com.
Got the question.
I've seen you mention a batch file a couple of times in articles.
What is that and what is it good for?
Files or command files as they're often
called, also are basically nothing more than text files that contain a series
of instructions to be executed within a Windows command prompt.
In other words, it's kind of a small computer program.
Now, what I want to do is I'm going
to show you a couple of simple examples to give you just sort of a sense for some
of the kinds of things you can do with batch files.
To do that, we'll switch over to Windows Eleven.
What I'm going to start with is something very simple.
We are going to windows are run and you can see it.
Remember that I ran notepad already.
That's what we're going to run here.
I'm going to enter the following
dir /O:-d.
Now I realize that is so much voodoo, so much magic.
This is an old style directory listing command from the Windows command prompt
that essentially says show me the directory.
Show me all the files in the current directory.
O means order them by colon is a separator, d means the date of the file,
and the minus sign means show them in reverse order.
So what I'm going to do is file save as
we're going to notice what folder we're in.
This says this PC Documents,
which if we click in it, you can see it says the Documents folder.
In reality.
I'll show you where that is on the disk in a moment.
What I'm going to save it as is tdir CMD.
In other words, the tdir is just me making up a name.
And rather than the Dir command,
I used tdir because it's a timebased Dir command.
CMD is the file extension that tells Windows this is a batch file.
BAT is also acceptable.
It's a very minor difference
that honestly, you'll never run into unless you're
working with some really old compatibility issues.
CMD is preferred these days for Windows command prompt.
So having saved it then as tdir.cmd, I'm going to run a Windows command prompt.
I'm going to do that this time by right clicking here.
In my case, I happen to have Windows Terminal.
I'm going to use admin because one
of the examples coming up is going to require admin privileges.
So what I have here is Windows PowerShell.
That is not what I want.
That is not what we are doing.
What I want instead is to open a new tab as command prompt.
That's what we're doing.
Windows PowerShell, as nifty as it is, runs a different language.
You can run CMD files in it, but I want to keep things clear.
CMD files and Bat files are intended
for the Windows command prompt CMD exe if you happen to run it directly.
But it's not PowerShell.
So now we are in this users ask Le folder. That's users.
And then my login name for this machine, the Documents folder, is here.
But of course, this one is empty.
And that's because I'm using OneDrive to back up this folder,
which means the Documents folder we care about is in Onedrive/documents.
And there you'll find tdir.cmd
so what I'm going to do is I'm going
to change directories to make that the current folder.
Now, to run tdir, all I really need to do is type Tdir and hit enter.
Because it's in the current folder,
it looks there first to see if there's a command called tdir.
If there is, that's what gets run.
So what we've done now is we've just run this batch file we've created.
What it did is it ran the Dir command
with these parameters, and the result, of course, is a listing of the files
in that folder in reverse chronological order.
So that's all well and good, but why would we ever want to do this?
Well, like I said,
this is an oversimplified example of some of the things you can start to do with it.
What I'm going to do now is actually give
you something I consider to be relatively useful.
I'm going to go back to notepad.
I'm going to create a new file.
And into this file, I've typed very quickly through the magic
of copy paste the following three commands net stop spooler a delete command
with a fairly long parameter of what to delete, and then net start spooler.
If you have had printer queue problems,
you've probably seen the article, my printer queue is stuck.
How do I print anything?
This sequence of three commands clears the print queue.
This does require that you be running in administrative command prompt.
And that's fine.
That's why I ran the administrative command prompt earlier.
But the point is that rather than having
to type each of these three commands in separately every time,
you need to clear your print clue, all you need to do is save this.
And I'm going to give it a different name.
Save as ClearPrint.
And again, CMD.
That's important.
If you just want to save clear print
without doing the CMD, it would actually save it as a TXT file.
And that is not a batch file.
Windows will not recognize that as a batch file.
It's important to include the extension CMD.
So now I have clear print on CMD.
If I go over to the Documents folder where these things were getting saved,
and I just do a regular dur, there you'll see ClearPrint CMD.
If I now run clear print CMD, it will clear the print queue.
It's stopping the spooler,
it's deleting the files, and it's starting the spooler again.
That's a lot easier than typing those
three commands in individually every time you need them.
And as you can see, clear print is probably something that's
a lot easier to type, and a lot easier to remember than
that complicated delete command there in the middle where we're deleting
something buried in the Windows directory structure.
So that's one example of another really useful way that batch files can help.
They can combine multiple commands that are executed in sequence.
So there's another example
in the companion article with this video that shows you how batch files can take
parameters for advanced batch file usage that can be kind of handy.
I'm not going to demonstrate it here.
I'll point you at that article for it.
It's askleo.com/3688.
But the point there is that batch files have a number of constructs that make them
pretty close to full on programming languages.
If you've ever heard the term scripting language, that's what we're doing here.
Batch files are more correctly referred to as scripts.
Do this, then do this, then do this.
That's all they really boil down to.
They have all sorts of constructs you can
loop, you can exit, you can test, you can do a number of different things.
But the point is, for very basic usage,
like this print cue clear that we just did, it can be a really simple and useful
thing to save you a lot of time and avoid a lot of errors in the future.
So the question often comes up, great, what can I do in a batch file?
I mean, you've showed me a few things.
I don't know where they came from.
The bottom line is that anything that you can run using the Windows are key in.
This Run dialog box can be run from within a batch file.
What does that mean?
How do you make that happen?
Well, there are two concepts at play here.
One is that Windows has this concept of what's called a path.
Whenever you type something into either
the Run dialog or the Windows command prompt, it searches along this path,
this list of folders for something that matches what you just typed in.
If it finds it in the first place, it finds it, that's what it's going to run.
So what that means is that you can put your batch file in one of these locations.
Cwindows is one example of such a placewindowsystem.
32 is where all of Windows built in utilities come from.
You get the idea it's a long list.
If I type Path,
you can see the list of folders that Windows will look at in turn.
As soon as I type something like that, start spooler.
What Windows is doing is it's walking that list of folders windows System 32,
Windows System 32, WBEM, and so on, looking for.
Net.exe, net.bat, net.cmd.
Net, a number of other file extensions.
As soon as it finds one, in this case,
it finds net exe somewhere in that path, it runs it and it's ready to go.
So if you were to put your own batch files
in, say, System 32, or just windows, they will get found automatically without
you having to specify the full path to the batch file.
But that's the other thing that can happen
is that you can specify the full path to whatever it is you want to run.
I'm going to do that.
You can see that I'm here in users as OneDrive documents.
What I'm going to do is I'm going to use
file completion or file name completion to make this happen.
So I clear the screen here so we can see what I'm going to do.
I will type Program Files and I'm just
using the tab key to automatically fill out the Rest. Program files.
Microsoft Office, root, as it turns out.
Let's see, there's a number of different things here.
Office 16, I think was what I want.
And there I can go with windward.exe.
So you can see I have typed the full path to a program that is not on the path.
It's not going to be found automatically.
In fact, if I were to just type WinWord, nothing would happen.
But if I were to double click on the WinWord icon or the Word icon
in my Start menu, well, that's already hard coded this exact path.
What that means is that if I just hit
enter on this full path, it's now going to run Word.
Anything that you can do that to, anything on your machine that you can just
enter and have it run, you can use in a batch file.
To be clear, I've really just scratched the surface.
There is so much more that you can do with batch files.
And as I mentioned earlier, if you really want to get into this kind
of scripting, PowerShell brings a whole nother level.
It's a different language,
but a whole other level of both power and complexity to your ability to script
things using a text file or command file type structure.
If that's interesting to you, great.
That's where you want to start looking.
But in the meantime, if you just want to make that print queue
easier to clear, well, now you know how to throw three or more
commands into a batch file and have it be something simple.
Hope that was helpful.
For updates, for comments, for related links and more visit
AskLeo.com/3688. I'm Leo Notenboom and this is Askleocom.
Thanks for watching.