Using Batch Files, Part 1

by Barry Dysert
(last updated January 30, 2017)

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

Regaining Control of Your System

The Windows Task Manager is a useful utility that provides information about what's running on your system. You can also use ...

Discover More

Resetting a Web Browser to Its Default Settings

We can make a lot of adjustments to our Web browser over time. If you ever want to restore your browser to its original ...

Discover More

Copying Data with XCopy

XCopy is a file and directory copy utility built into Windows. If you have a lot of file management to do that can't easily ...

Discover More
More WindowsTips

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

Introduction to PowerShell

PowerShell is found on all modern Windows computers. You might think of it as the next step up from the command line .bat ...

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 three minus 2?

2017-01-30 11:36:59

Scott Renz

You can use the SHIFT command in the batch file to get more than ten parameters. If you have ten parameters, you can put the SHIFT command in the batch file and if you put echo %9 it will echo the tenth parameter.

shift
echo %9


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.