1.

Solve : Testing for & Enumerating %~* and %*?

Answer» HI All
Finally joined the community after spending incalculable amounts of time combing through the site & forums. Thanks to all for lending your expertise.

Although I create little 'batch' scripts regularly for deployments and various other TASKS, I often 'script' for fun. Today's one of those days. I'm writing a small .cmd file for my teammates to help them understand the various variables available at run time. Instead of writing out
Code: [Select]echo %%~0 = %~0
echo %%~1 = %~1
echo %%~2 = %~2
...
echo %%~9 = %~9

rem testing arguments
echo %%0 = %0
echo %%1 = %1
echo %%2 = %2
...
echo %%9 = %9
I was hoping I could leverage FOR /L instead to do something like:
Code: [Select]for /L %%a in (0,1,9) do (
if defined %~%%a echo %%~%%a = %~%%a
if defined %%a echo %%%a
)

I don't know if I can, and how to, test whether or not %1 or %~1 have been defined via for loop.

Its silly, I know. Any ideas?Tricky, but I think Ive done it
Code: [Select]@echo off
echo %%* = %*
for /L %%a in (0,1,9) do call :test %%a %%%%a
goto :EOF

:test
set p=
call set p=%%2
if "%p%"=="" goto :EOF
echo %%%1 = %2

Anyone have any improvements ??NICE - very interesting results.

This seems to work well for testing arguments but not when enumerating %~* during run time. For instance...

I execute the following:
Code: [Select]"C:\test\script.bat" "C:\test\script.bat" two three four five six seven eight nine
Which returns:
Code: [Select]%* = "C:\test\script.bat" two three four five six seven eight nine
%0 = "C:\test\script.bat"
%1 = "C:\test\script.bat"
%2 = two
%3 = three
%4 = four
%5 = five
%6 = six
%7 = seven
%8 = eight
%9 = nine
This works great for checking arguments and the like. However in situations where you might want to strip the quotes around the argument, you would have to use %~1, %~2 etc. Unfortunately there doesn't seem to be a way to do that. Dropping %~ in front of %2 fails altogether, and adding an extra % in front of that results in a literal output: %~two %~three etc.

The batch works as is but was just hoping to add some logic for argument checking. I was hoping I could do something like this but I can't get it to work properly even after
Code: [Select]:EVALARGS
if "%~0"=="" goto :NEXT
echo %0
shift
goto :EVALARGS

According to http://www.robvanderwoude.com/if.php I should be able to use the above example or even [%~0]==[] and "%~0"=="/?" but I had trouble there.Quote from: Phylum on May 25, 2011, 11:43:34 AM
if "%~0"=="" goto :NEXT

But %0 is a special argument. and is unaffected by SHIFT, which dumps %1 and replaces it with %2 and so on until all non-null parameters are exhausted.

* You do know about ~d, ~p, ~n, ~x and the others?


However...

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /L %%a in (0,1,9) do (
call set param=%%%%a
if not "!param!"=="" (
call echo %%%%%%a is %%%%a and %%%%~%%a is %%~%%a
)
)

Called with 5 parameters

Code: [Select]C:\Test>nparams005.bat cat dog horse cheese "cake"
%0 is nparams005.bat and %~0 is nparams005.bat
%1 is cat and %~1 is cat
%2 is dog and %~2 is dog
%3 is horse and %~3 is horse
%4 is cheese and %~4 is cheese
%5 is "cake" and %~5 is cake

Hi Salmon Trout

I must have been out of my MIND when I put %~0. As foolish as it looks now, I meant %~1 through 9. Thank you for that!

Your solution works perfectly! I struggled with trying to do what you did in the if statement, very creative!

In case anyone is ever curious about argument counting without using shift
Code: [Select]set argcount=0
for %%a in (%*) do Set /A argcount+=1


Discussion

No Comment Found