DOS and Batch Files

Testing for empty strings, spaces and the length of a string

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

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

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

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.

DOS has no native way to test for the length of string so what happens in batch files is that the last character of a string is removed and a counter increased 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

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

@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 30th December 2011, last modified 30th December, 2011


GoStats stats counter