Using Batch Files, Part 2

Written by Barry Dysert (last updated October 18, 2021)

6

This tip builds on the tip titled Using Batch Files, Part 1, so you might want to check that out before proceeding. Specifically, we're working with a file called "C:\Bat\Fun.bat", so display a command prompt window and change your default directory to be C:\Bat. Launch notepad in order to edit Fun.bat, which currently looks like this:

@ECHO OFF
REM This file demonstrates the use of a few command-line statements
ECHO Hello. This is my Fun.bat file.
DIR %1

The last command in the file is the DIR command. It creates a directory listing of the folder specified as the first parameter that was passed to the batch file. If you don't want the listing displayed on the screen, you can use the ">" character to direct the output to a file. So let's modify Fun.bat to accept two parameters. The first will be the folder whose directory listing you want to obtain and the second will be the file where you want this output to go. Your batch file should look like this:

@ECHO OFF
REM This file demonstrates the use of a few command-line statements
ECHO Hello. This is my Fun.bat file.
DIR %1 > %2

You invoke as before except now you specify the second parameter on the command line:

C:\Bat> Fun.bat C:\Temp C:\Temp\Dirlist.txt

Execute the batch file and you end up with a file called C:\Temp\Dirlist.txt that holds the directory listing of all of the files in the C:\Temp directory.

Say that you have thousands of files in your C:\Temp directory. Instead of going through the long list, you may only want to know if a particular file is in there and how big it is. You can do this by "piping" the output of the DIR command to a new command, FIND.

In general, most commands that produce output data (such as what is produced by DIR) can be used as input to another command—all on the same line. This is called "piping," and the character used for this operation is the vertical bar ("|"). The command expecting input, FIND in our case, appears to the right of the "pipe" character (the vertical bar). So let's replace our previous DIR command with one that pipes its output to FIND:

@ECHO OFF
REM This file demonstrates the use of a few command-line statements
ECHO Hello. This is my Fun.bat file.
DIR %1 | FIND "Dirlist"

The effect of this change is to execute the DIR command on the specified folder, but send its output to the FIND command, which in turn searches that output for the string "Dirlist" and outputs to the screen the results. Note that we will only pass one parameter—the folder name on which to do the DIR. If you forget and happen to specify a second parameter, it's no problem. Since there is no "%2" in the batch file any additional parameters will be ignored.

The last things to cover in this tip deals with time. There is a command called "TIME /T" which displays the current time. There is also a command called TIMEOUT which causes the batch job to pause for the given number of seconds. Finally, there is another special character you can use in a batch file, viz., the ampersand ("&"). It concatenates together two (or more) commands that appear on the same line. Let's use these commands to display the current time, wait five seconds, and write the new time to the screen. The batch file should look like this:

@ECHO OFF
REM This file demonstrates the use of a few command-line statements
ECHO Hello. This is my Fun.bat file.
DIR %1 | FIND "Dirlist"
ECHO The current time is & TIME /T
TIMEOUT 5
ECHO The new current time is & TIME /T

That's a little more about batch files. There is so much more they can do, but I'd like to gauge how much interest there is before writing much more about them. If you are so inclined, please indicate in the comments, below, whether you would like to continue learning more about batch files.

 This tip (13103) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

M8 Free Clipboard

If you've ever found it tedious to do copy/paste with one piece of information at a time, you'll really like M8 Free ...

Discover More

Using Windows DVD Maker

You can use the built-in DVD Maker to create slide shows and views of your own photographs and videos. This tip tells you ...

Discover More

Using Robocopy to Mirror Directories

It's simple to use Robocopy when dealing with entire directories. Dealing with directories is what it does best! This top ...

Discover More
More WindowsTips

Using the SHIFT Statement

Sometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about ...

Discover More

Creating a Simple Batch File

One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...

Discover More

Using the FOR Statement

In another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2021-10-19 22:37:37

Tomek

I started using computers when they used DOS. Batch files were a big thing then. What I learned then is still usable, but now and then I need to refresh my memory, and as an old dog would appreciate learning new tricks.

Keep the series going.


2021-10-19 21:10:21

Peter

Hello Barry,
I would really enjoy if you continue your tips about batch files. This revives my long-forgotten interest for using batch files..


2021-10-19 12:03:43

Bob Ama

Yes, I'd like to learn more about some common uses of .bat files. What can bat do for me?


2021-10-18 21:14:45

Sandeep

Please carry on your discourse on batch files. It's very interesting.


2021-10-18 14:55:06

Leonard Berkowitz

I am enjoying this series. Very valuable. Please do continue. Thanks.

(I hope that people notice your request for comments. I missed it the first time through)


2021-10-18 08:43:53

Thierry

Hello Barry,
Yes please, carry on elaborating on batch files and command line statements! In the past I have looked in vain for ways to introduce variables in the names of files to which command output was sent, now I see a glimpse of the solution - Thanks!


Newest Tips