Tuesday 29 April 2008

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

5. The %0 argument

I mentioned in my previous post that you could 'read' arguments using %1 through %9 (with or without the ~).

There is another argument you can get: %0. Of course, numbering had to start at 0, we are in geek world after all, right?

The first argument (%0) is simply the first thing that appears on the command line, which is the command itself.

The following batch code illustrates this:

@echo off

:init_vars
SET cmdln=%~0
if "%cmdln%"=="" goto no_cmdln
goto show_cmdln

:show_cmdln
echo Argument 0: %cmdln%
goto eof

:no_cmdln
echo No argument %%0.
goto eof

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

:blackhole


Executing this will show the complete path to the current batch file.

Now let's call this same batch from within another simple batch:

@echo off
:: show_arg0 is the first batch file's name
call "show_arg0"


This will show:

Argument 0: show_arg0


Note that we never actually get to the no_cmdln condition!

Well, it's getting late I am afraid, but do come back for the next post, we'll be seeing how to use a variable number of arguments, and how to retrieve more than 9... so, until next time.

No comments:

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