Monday 28 April 2008

Batch Programming Tip #04: Working with arguments - Part 1

This is less a tip than a succinct how-to.

1. The basics
Passing arguments to a script (or subroutine) is very simple: add the arguments just after the call on the same line. The arguments are separated by space. If you need to pass an argument which itself contains one or more spaces, make sure to enclose it using quotes.

Let's write a short example. The idea is simply to write two words which are passed as arguments.

2. Our first write words script
First let's clear up the caller syntax by writing the following to call_write_words.bat:

call write_words hello "hello world"

We pass two arguments:
hello
"hello world"

Note that the second argument is quoted as it contains a space.

That was the easier part, now let's look at how we get hold of the arguments and then print them on screen. To do this we will create a second batch file containing the code just below which is probably the simplest form of getting and printing two arguments.

To get the value of the arguments, use %1, %2, ..., %9:

@echo off

:action
echo Sentence1: %1
echo Sentence2: %2
goto eof

:eof
echo Press a key to quit...
pause > NUL
goto blackhole

:blackhole

This will output:

Sentence1: hello
Sentence2: "hello world"
Press a key to quit...

With our current example, if you don't provide values for those two arguments however, the program will ouput:

Sentence1:
Sentence2:
Press a key to quit...

In this particular case, it doesn't really matter as empty values will not cause an error (and/or make the program halt or falter in any way).

Mostly though you will want to check the values that are stored in your arguments and you may even provide default values in some cases.

3. Checking arguments
In the following batch program, we will create two variables to hold the values of the arguments passed to it. If no value is provided a default value is used instead.


@echo off

:init_vars
SET default=No sentence passed!
SET sentence1=%1
SET sentence2=%2
if "%sentence1%"=="" set sentence1=%default%
if "%sentence2%"=="" set sentence2=%default%
goto action

:action
echo Sentence1: %sentence1%
echo Sentence2: %sentence2%
goto eof

:eof
echo Press a key to quit...
pause > NUL
goto blackhole

:blackhole


Important note: to compare empty string values, enclose the string with the character of your choice. I usually use the " character but the [ and ] characters are also often used.

For instance:
if "%1"=="" ...
or
if [%1]==[] ...
have exactly the same meaning, the bottom line being that if the condition returns true, your %1 must be empty. In that case, use the default value you previously prepared or exit with an error message as needed.

Using:

call write_words "hello world"


You get the following output:

Sentence1: "hello world"
Sentence2: No sentence passed!
Press a key to quit...


You may be wondering how to get rid of the quotes around your "hello world" string. Indeed, you have to add the quotes to passe the argument but you may just have been meaning to pass "hello world" without the quotes, except that is technically impossible.
The good news is there's a nifty little feature that let's you do this: instead of using %1, %2, ... %9 use %~1, %~2, ... %~9.
Using this in our first simplified version, we get the following:

@echo off

:init_vars
:: added the ~
SET sentence1=%~1
:: added the ~
SET sentence2=%~2
goto action

:action
echo Sentence1: %sentence1%
echo Sentence2: %sentence2%
goto eof

:eof
echo Press a key to quit...
pause > NUL
goto blackhole

:blackhole

A much nicer result without the quotes.
It may seem a little superficial here but quotes can be a realy nuisance (when working with file names, and paths for instance) and you would mostly "strip" your arguments even if to add quotes later on when needed.

4. The argument wildcard
Finally, for today, another way of handling arguments is to use the argument wildcard %*. This will get all the arguments in one go.

Let's prepare our call:

@echo off
call write_words_star well, that will be it for today, tchuss until next time...


And now handle the call in a separate batch (write_words_star.bat):

@echo off

:init_vars
goto action

:action
echo Arguments: %*
goto eof

:eof
echo Press a key to quit...
pause > NUL
goto blackhole

:blackhole

The console will show the following:

Arguments: well, that will be it for today, tchuss until next time...
Press a key to quit...


Well, that really will be it for today. Until next time...

No comments:

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