Thursday 24 April 2008

Batch Programming Tip #01: Variables and spaces

Well, this is probably the most basic of all tips. It's also one that will save you loads of time if you got it wrong in the first place (as I did...). Most programming languages don't worry too much about spaces unless they are part of a string.

For instance, in PHP you could declare:
$my_var = "hello world"; // there are spaces around the = sign
or
$my_var="hello world"; // there are no spaces around the = sign
indescriminately as both declarations have the same meaning.

Try the following in batch:

@echo off
SET my_var=world
echo Hello %my_var%
pause > NUL

This will work as expected and output hello world to the standard output (which would normally be the console). Notice we do not use quotes.

If you try this second example, however, you may be in for a surprise.

@echo off
SET my_var = world
echo Hello %my_var%
pause > NUL

Indeed the script doesn't work as expected: it just outputs Hello followed by ...nothing. The reason for this behaviour is that batch programming variable names may contain spaces. Yup, scary, huh?

So to make the second example "work", we need to add the space in the variable reference as follows:

@echo off
SET my_var = world
:: there is a space before the second % below
echo Hello %my_var %
pause > NUL

This will output Hello  world. Notice that there are two spaces between hello and world because the value which is assigned to the %my_var % variable starts immediately after the = sign.

So the next fix is to remove that leading space:

@echo off
:: the space between = and world has been removed
SET my_var =world
echo Hello %my_var %
pause > NUL


Let's finish with a working example of a variable name containing spaces.

@echo off
:: ask the user to enter first name
SET /P first name=Please enter your first name:
SET hello=Hello
echo %hello% %first name%
pause > NUL


Enter your first name and hey presto, it hellos you as planned.

Although I would never suggest using variables with spaces as they can lead to serious confusion, this tip would usually be a good place to start when variables seem to unset themselves like by (black) magic between one line and the next.

Well, that will be it for today...

@echo off

:::
:: Init variables
:

:init_vars
SET goodbye=See you later,
SET end_goodbye=Thanks for reading...until next time ;-).
SET anonymous=Anonymous
SET attempts=0
SET max_attempts=3
goto ask_for_first_name

:::
:: Ask for first name
:: Loop for max_attempts if necessary or give up.
:

:ask_for_first_name
SET /P first name=Please enter your first name, so I can be polite and say goodbye:
SET /A attempts+=1

if "%first name%"=="" (
:: LSS is definitely an Win NT trick
if %attempts% LSS %max_attempts% goto ask_for_first_name
SET first name=%anonymous%
)
goto say_goodbye


:::
:: Output goodbye
:

:say_goodbye
cls
echo %goodbye% %first name%
echo %end_goodbye%
goto eof


:::
:: Pause for reading
:

:eof
echo.
echo (Press any key to quit...)
pause > NUL

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory