Batch Files

String Lengths and Special Characters

Introduction

Batch files are incredibly useful but sometimes incredibly frustrating. Testing if a string is empty or contains special reserved characters rquires alittle thought and special techniques and there is no built in function for finding the length of a string at all. This page looks at some of these problems.

My thanks to Philippe Ferrucci and Steve Vinge who both emailed me to point out some silly mistakes I made in the first version of this page.


Creating a String

Variables are created in batch files using the "set" command, but there are rules and special considerations to be folloewd even using this simple command. Considerthe following batch file:

@echo off
set str="This is a string"
echo %str%
echo.
set str= This is a new string
echo %str%
echo.
set str=Apples ^^^& oranges
echo %str%

The output from this batch file is:

"This is a string"

 This is a new string

Apples & oranges

Unlike other scripting languages, the string should not be delimited as the delimiters themselves become part of the string. There should be no spaces before or after the "=" sign.

Some characters such as & \ < > ^ and | need to be escaped when added as a character to a string. The method of escaping them in batch files in at least Windows 11 is different than in otherwaise excellent sites such as Ron van der Woude's Scripting Pages and SS64, and I have found what works most of the time is to escape the character with a triple caret, "^^^".


User Input

User input can be obtained by using a line such as:

set /p str=Input your string:

Which produces:

set /p str=Input your string:

Which produces

Input your string:

This is fine except when a user inputs a character such as & \ < > ^ and |, all of which produce unexpected results when echo'd to the screen.

Conside the following short batch file:

@echo off
set /p str=Input your string:
echo.
echo "%str%"

If the user enters something like:

apples & oranges > bananas

Then having wrapped the variable in quotes, the following is displayed on the screen:

"apples & oranges > bananas"

Instead of the the message "'oranges' is not recognized as an internal or external command, operable program or batch file." that would appear if just echo %str% was used.

By enclosing the variable in quotes, the variable can be rendered "safer" by escaping the special characters and removing the quote marks. This can be achieved by using:

set str="%str%"
set str=%str:&=^^^&%
set str=%str:>=^^^>%
set str=%str:"=%
echo %str%

which gives:

apples & oranges > bananas


Comparing Strings

There is plenty of help about using the comparison operators in If statements by typeing if /? at a command prompt, so this is just a quick summary.

To make the comaprison case insentitive the /I switch can be used as in the following batch file:

@echo off
set oldstr="This is a string"
set newstr="This Is A String"
echo.
echo Comparing %oldstr% with %newstr%
echo.
echo Case sensitive:
If %oldstr%==%newstr% (echo The strings are the same) else (echo The strings are not the same)
echo.
echo Case insensitive:
If /I %oldstr%==%newstr% (echo The strings are the same) else (echo The strings are not the same)

Which outputs:

Comparing "This is a string" with "This Is A String"

Case sensitive:
The strings are not the same

Case insensitive:
The strings are the same


Defined and Undefined Variables

Look at this batch file:

@echo off
set spc= ::a single space
set empty=""
set quote="""
set amp=&
set spcamp= &
set escamp=^&
set def=

if defined spc (echo spc is defined else (echo spc is not defined)
if defined empty (echo empty is defined) else (echo empty is not defined)
if defined quote (echo quote is defined) else (echo quote is not defined)
if defined amp (echo amp is defined) else (echo amp is not defined)
if defined spcamp (echo spcamp is defined) else (echo spcamp is not defined)
if defined escamp (echo escamp is defined) else (echo escamp is not defined)
if defined def (echo def is defined) else (echo def is not defined)
if defined nodef (echo nodef is defined) else (echo nodef is not defined)

The output of it is:

spc is defined
empty is defined
quote is defined
amp is not defined
spcamp is defined
escamp is defined
def is not defined
nodef is not defined

The variable def is initiated in the above batch file but has no content so it is not defined. nodef isn't initiated at all so that is also not defined

The Ampersand (&)

The & character is a special character at the command line and used alone means carry out an instruction before the character and then carry out the next instruction regardless of the result of the first command.

A double ampersand (&&) means carry out the the instruction before the characters and if successful, then carry out the instruction after them.

In the above batch file the variables are being set as follows:

amp is undefined as there's nothing between the instruction and the & character. In effect, amp is set the same was as def, just the empty variable is created and as there's no instruction after the ampersand, the batch file moves on to the next line.

spcamp is almost the same except for the space after the equals sign. spcamp is set to a single space which means it is defined, and again, as there's no instruction after the ampersand, the batch file moves on to the next line.

In escamp the & character is escaped by preceding it was a caret (^) and so escamp is set to a single & character which means it is defined.


Finding the Length of a Variable

Unlike other scripting languages, there is no command in batch files to get the length of a string. There are several different methods used to dind the length of a string but the one I used most is to copy the string into a temporary variable then decrease the number of characters in it by one. This increases a counter and ends when the temporary file is empty, that is it no longer defined.

The batch file to do this is simply:

@echo off
set mystr=This is a string
set tmpstr=%mystr%
set count=0

:loop
If defined tmpstr (set tmpstr=%tmpstr:~1%&set /A count += 1 & goto loop)

echo The string "%mystr%" is %count% characters long.

The result of this batch file is:

The string "This is a string" is 16 characters long.

This is correct but out of curiosity I tried the variables that were defined as true in the Defined and Undefined Variables section of this page. This returned the following results:

empty="" gave: nothing
quote=""" gave: nothing
spcamp= & gave: The string " " is 1 characters long. This is correct
escamp=^& The string "&" is 1 characters long. This is correct

It appears that if the string contains, or could contain, a special character, then another method must be found to find the length of the string.


Sources and Resources

:strLen (Dos Tips)
Batch Script - String length (Geeks for Geeks)
Escape Characters (Rob van der Woude's Scripting Pages)
Get String Length (Rob van der Woude's Scripting Pages)
Get String Length - How It Works (Super User)
How can I check if an argument is defined when starting/calling a batch file? (Stack Overflow)
How do you get the string length in a batch file? (Stack Overflow)
How to avoid cmd.exe interpreting shell special characters like > < ^ (Stack Overflow)
How to receive even the strangest command line parameters? (Stack Overflow)
How-to: Escape Characters, Delimiters and Quotes at the Windows command line (SS64)
How-to: Get the length of a String - strlen.cmd (SS64)
stringlength.cmd - just created - for anybody who can use it (SS64)
What is the proper way to test if a parameter is empty in a batch file? (Stack Overflow)