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.
This tip is part of a series that shows you how to create and use Windows batch files. It introduces a few commands and ...
Discover MoreOne of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide ...
Discover MoreThis tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...
Discover More2021-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!
Copyright © 2024 Sharon Parq Associates, Inc.
Comments