Batch File String Processing
As you gain experience with batch files, you're likely to find that you
need to manipulate text strings. You may need to prompt a user for a name
or password, process a list of files, or find a name in a phone list. All
of these are examples of string processing the manipulation of lines of
readable text.
CMD.EXE include several features that make string processing easier. For
example, you can use the INKEY and INPUT commands for user input; the
ECHO, SCREEN, SCRPUT, and VSCRPUT commands for output; and the FOR command
or the @FILEREAD function to scan through the lines of a file. In
addition, variable functions offer a wide range of string handling
capabilities.
For example, suppose you need a batch file that will prompt a user for a
name, break the name into a first name and a last name, and then run a
hypothetical LOGIN program. LOGIN expects the syntax /F:first /L:last
with both the first and last names in upper case and neither name longer
than 8 characters. Here is one way to write such a program:
@echo off
setlocal
unalias *
input Enter your name (no initials): %%name
set first=%@word[0,%name]
set flen=%@len[%first]
set last=%@word[1,%name]
set llen=%@len[%last]
iff %flen gt 8 .or. %llen gt 8 then
echo First or last name too long
quit
endiff
login /F:%@upper[%first] /L:%@upper[%last]
endlocal
The SETLOCAL command at the beginning of this batch file saves the
environment and aliases. Then the UNALIAS * command removes any existing
aliases so they won't interfere with the behavior of the commands in the
remainder of the batch file. The first block of lines ends with an INPUT
command which asks the user to enter a name. The user's input is stored
in the environment variable NAME.
The second block of lines extracts the user's first and last names from
the NAME variable and calculates the length of each. It stores the first
and last name, along with the length of each, in additional environment
variables. Note that the @WORD function numbers the first word as 0, not
as 1.
The IFF command in the third block of lines tests the length of both the
first and last names. If either is longer than 8 characters, the batch
file displays an error message and ends. Finally, in the last block, the
batch file executes the LOGIN program with the appropriate parameters,
then uses the ENDLOCAL command to restore the original environment and
alias list. At the same time, ENDLOCAL discards the temporary variables
that the batch file used (NAME, FIRST, FLEN, etc.).
When you're processing strings, you also need to avoid some common traps.
The biggest one is handling special characters.
Suppose you have a batch file with these two commands, which simply accept
a string and display it:
input Enter a string: %%str
echo %str
Those lines look safe, but what happens if the user enters the string "some
> none" (without the quotes). After the string is placed in the variable
STR, the second line becomes:
echo some > none
The ">" is a redirection symbol, so the line echoes the string "some" and
redirects it to a file called NONE - probably not what you expected. You
could try using quotation marks to avoid this kind of problem (see
Argument Quoting), but that won't quite work. If you use back-quotes
(ECHO `%STR`), the command will echo the four-character string %STR.
Environment variable names are not expanded when they are inside
back-quotes.
If you use double quotes (ECHO "%STR"), the string entered by the user
will be displayed properly, and so will the quotation marks. With double
quotes, the output would look like this:
"some > none"
As you can imagine, this kind of problem becomes much more difficult if
you try to process text from a file. Special characters in the text can
cause all kinds of confusion in your batch files. Text containing
back-quotes, double quotes, or redirection symbols can be virtually
impossible to handle correctly.
One way to overcome these potential problems is to use the SETDOS /X
command to temporarily disable redirection symbols and other special
characters. The two-line batch file above would be a lot more likely to
produce the expected results if it were rewritten this way:
setdos /x-15678
input Enter a string: %%str
echo %str
setdos /x0
The first line turns off alias processing and disables several special
symbols, including the command separator (see Multiple Commands) and all
redirection symbols. Once the string has been processed, the last line
re-enables the features that were turned off in the first line.
If you need advanced string processing capabilities beyond those provided
by CMD.EXE you may want to consider using the REXX language. Our products
support external REXX programs for this purpose.
Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs