Using Batch Files, Part 1

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

1

Windows batch files are great if you want to perform several tasks in quick succession, or if you want to do things to several files in a row, or if you simply don't want to use the GUI interface to perform some tasks (e.g., performing unattended tasks). This tip builds a bit on the tip titled Creating a Simple Batch File, so you might want to check that out before proceeding.

If you're going to be using batch files much, it's a good idea to create a folder to hold them all. I have such a folder on my system called "C:\Bat". I also have a "C:\Temp" folder, which serves as a temporary location for various files I deal with on an ongoing basis. This tip makes use of both of these folders, so if you don't have them already, I suggest that you create them.

The main purpose of the batch file we're going to create is to introduce some command-line features more than to do anything very useful, so let's give our batch file the name "C:\Bat\Fun.bat". Create the empty file, and launch Notepad to edit it. You should also display the command prompt window so that you can run the batch file from the command line instead of Windows Explorer. (Displaying the command prompt window is covered in another tip.)

As you develop batch files you may wish to add comments to them to remind yourself of what they do and how they do it. To add a comment, use the REM statement (short for "REMark") or put a double colon ("::") at the beginning of a line. Comment lines aren't processed when the batch file is invoked; they're simply there as documentation for whoever is looking at the file.

Another simple command is the ECHO command, which does get processed and displays on the screen whatever text follows ECHO. We can use these two commands in our batch file as follows:

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

Run this file from the Command Prompt window simply by typing its name:

C:\> \Bat\Fun.bat

You'll notice when you run this file that the lines are displayed on the screen. This is because the default behavior for batch files is to display the file's contents as it runs. To prevent this from happening you can precede each statement with the "@" symbol like so:

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

Now when you run it, you'll only see what the ECHO statement says to display. Instead of preceding each statement with the "@" symbol, you can turn off displaying the file's contents by placing an "@ECHO OFF" statement at the beginning of your file. So now the file 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.

You can pass parameters to a batch file when you invoke it so that it can work on variable information at run time. You pass parameters simply by specifying them on the command line after the name of the batch file you want to run. You then use the passed parameters in your file via the special symbols "%1", "%2", etc., up to "%9" (you can pass up to nine parameters to a batch file). Let's say, for example, that you want your file to display a directory listing of whatever folder you specify at run time. Edit your batch file to include the DIR command, like so:

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

Then pass the parameter on the command when it's invoked:

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

This produces a directory listing of your C:\Temp folder.

This tip has introduced a few new concepts about batch files. If you want to go a bit deeper, just search on the WindowsTips site for additional clues on how you can use batch files.

 This tip (9984) 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

Removing a Place from the Recent Places or Quick Access Lists

Some Windows features can be both time savers and privacy concerns at the same time. The Recent Places list and the Quick ...

Discover More

Renaming Multiple Files at the Same Time

It's possible to rename multiple files at the same time, but the new names of the files may leave a bit to be desired. ...

Discover More

Recovering a Forgotten Administrator Password

Forgetting your administrator password may be cause for panic because there is no supported way to discover it. Before ...

Discover More
More WindowsTips

Using Batch Files, Part 2

This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...

Discover More

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

Using Batch Files, Part 4: The CALL Statement

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

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 five minus 3?

2021-10-19 04:17:34

sandeep

Great post on batch files. Keep going.


Newest Tips