Using Batch Files, Part 2

by Barry Dysert
(last updated February 6, 2017)

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 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Creating Your Own File Folders

In the general course of using your system, you will likely create many new files. It's a good idea to organize your files ...

Discover More

Using Offline Files

If you need access to files on a network but aren't always on the network, you may find that the offline files feature of ...

Discover More

Using Process Monitor

A very useful tool in diagnosing what is going on with processes and/or files is the Process Monitor tool from Sysinternals. ...

Discover More
More WindowsTips

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into and ...

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 as ...

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 variety of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 seven more than 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.