Delimiters and Batch Files

The example on page 402 of Windows Command-Line Administration Instant Reference produces the correct result. You see the result of passing various bits of information between two batch files. However, as someone wrote to me recently, the output from the Batch2.BAT file isn’t the result you might expect. Instead of showing the entire %PATH% environment variable, you see just the first part of this environment variable as shown here.

BatchFile01

The reason you only see C:\Program as the environment variable output is the fact that %PATH% contains delimiters. There are a number of characters that the command prompt uses as delimiters, separators between elements in a single string. My testing shows that the space, tab, and semi-colon are three characters that always act as delimiters within a batch file. Of course, delimiters are extremely useful when you want to use one string to hold multiple elements for processing, but they can also cause interesting results, such as in this case where only part of the %PATH% environment variable appears in the output.

Of course, you’re probably asking how to obtain the entire environment variable as output. A simple change to Batch1.BAT makes this possible as shown here.

@ECHO OFF
Call Batch2.BAT
Call Batch2.BAT Passed %1 "%PATH%"
ECHO In Batch 1
GOTO :EOF
ECHO Goodbye

Notice that %PATH% now appears within double quotes. This change tells the command processor not to process the information within the %PATH% environment variable as separate entities. With this change you see the following output.

BatchFile02

Now you’re seeing the entire environment variable in the output. It’s important to note this difference in processing strategies when creating batch files of your own. What other batch file quirks have you encountered. Let me know at John@JohnMuellerBooks.com.

True Peace

Today I thought I’d share a poem I’ve written with you.

True Peace

Peace, lend a hand.
Peace isn’t the absence of war,
It isn’t the result of war.
Peace begins within,
but it ends without.

Peace, lend a hand.
You cannot win peace alone,
everyone labor and groan.
Envy, greed, and lust,
these make peace a bust.

Peace, lend a hand.
All your neighbors you must see,
as your friends with spirits free.
Your lives intertwined,
spirit, body, mind.

Peace, lend a hand.
All peace with God must begin,
a peace without is too thin.
Look above to see,
what your life could be.

Copyright John Paul Mueller, 2011