Batch Files

String Length & Empty Strings

Introduction

Sometimes it is necessary to check the string a user has given as input for a batch file, if a string contains nothing or an empty space or to count the number of characters in a string.

This page is not a comprehensive guide to batch files and strings but does contain routines that I have found useful.

Testing if one string is equal to another

Sometimes you need to test user input. Testing to see if one string is equal to another is easy using batch files


set /p name=What is your name?
if %name% == ray echo Your name is %name%

Unfortunaely, only some versions of DOS are case sensitive when doing this test, so if the user capitalizes the name then the IF test will fail. For those versions that do support case sensitivity then the /I switch can be used to force case insensitivty. I have found that /i also works.

if /I %name% == RAY echo Your name is %name%

Many thanks to Philippe Ferrucci who took the trouble to email me about a silly mistake I made when originally writing this page (I put %I instead of /I - duh).

Ron van der Woude's Scripting Pages has some great routines for converting strings to upper, lower and even mixed case.

Testing for null or empty strings

In order to test for a string that has no length or made up a single space character then BOTH the variable and the character tested for must be delimited.


@echo off
echo.
:userin
set name=
set /p name=What is your name?
if "%name%" == " " goto userin
if "%name%" == "" goto userin
echo.
echo. Your name is %name%
echo.

Testing for the length of a string

DOS has no native way to test for the length of string so the usual method to get the length of a string is to repeatedly remove the last character of a string and increment a counter by 1. When the string is no longer defined then the length of the string is whatever is in the counter.

This code is based on a batch file found at Phipster (Internet Archive)


@echo off
echo.
:userin
set name=
set length=0
set /p name=What is your name?
set tmpstr=%name%
echo.
echo. Your name is %name%
echo.
:loop
if defined tmpstr (
REM shorten tmpstr string by one character
set tmpstr=%tmpstr:~1%
REM increment the string count variable %length%
set /A length += 1
REM repeat until string is null
goto loop
)
echo. Your name is %length% characters long
if %length% LSS 3 (
echo.
echo. Your name is not long enough. Try again...
echo.
goto userin
)
echo.

There is a way to shorten this code by putting the entire If DEFINED loop on one line


@echo off
:userin
echo.
set name=
set length=0
set /p name=What is your name?
set tmpstr=%name%
echo.
echo. Your name is %name%
echo.
:loop
REM shorten tmpstr string by one character, increment the string count variable %length%, repeat until string is null
if defined tmpstr (set tmpstr=%tmpstr:~1%&set /A length += 1&goto loop)
echo. Your name is %length% characters long
if %length% LSS 3 (
echo.
echo. Name is not long enough. Try again...
echo.
goto userin
)
echo.

Any multi-line command in batch files can be shortened using the & character. Leaving spaces either side of the & character, espcially before it, can give odd results.


@echo off
:userin
echo.
set name=
set length=0
set /p name=What is your name?
set tmpstr=%name%
echo.
echo. Your name is %name%
echo.
:loop
REM shorten tmpstr string by one character, increment the string count variable %length%, repeat until string is null
if defined tmpstr (set tmpstr=%tmpstr:~1%&set /A length += 1&goto loop)
echo. Your name is %length% characters long
if %length% LSS 3 (echo.&echo. Name is not long enough. Try again...&echo.&goto userin)
echo.

To further shorten the file then the echo. commands used for formatting the output can be removed and the variable names reduced to single characters. The REM lines can also be removed. All of this shortens the code but doesn't do anything at all for readability. Sometimes it is better to sacrifice brevity for readability.


@echo off
:userin
set a=
set b=0
set /p a=What is your name?
set c=%a%
echo. Your name is %a%
:loop
if defined c (set c=%c:~1%&set /A b += 1&goto loop)
echo. Your name is %b% characters long
if %b% LSS 3 (echo. Name is not long enough. Try again...&goto userin)
This page created December 30, 2011; last modified January 26, 2022