JSI Tip 7924. How can I interrogate the last parameter of a call statement when you have more than nine (9) parameters?

Jerold Schulman

April 12, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today


In tip 6792 How many parameters can I pass to batch file, we learned that their is no limit to the number of parameters you can pass to a batch file, but the highest parameter that you can address is %9.

I have scripted LastNNP.bat to return the content of the last non-null parameter of any call.

The syntax for using LastNNP.bat is:

call lastnnp lastval [p1 p2 p3 .... pn]

where:

lastval is a call directed environment variable that will contain the contents (string) of the last parameter, and p1, P2, P3, and pn are the parameters.

Examples:

If your batch file was called, as in:

call YourBatchFile AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM

then

call lastnnp lastval %*

If you use a FOR command to parse for an unknown number of parameters, and you are only interested in the last parameter, as in:

for /f "Tokens=2-27" %%a in ('rsm view /tlibrary /guiddisplay^|Findstr /i /c:"Ultrium"') do (
call lastnnp lastval %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z
)

LastNNP.bat contains:

@echo offif {%1}
{} @echo Syntax: call lastnnp lastval [p1 p2 p3 .... pn]&exit /b 1setlocalset valname=%1shift:loopif {%1}
{} goto foundset last=%1shiftgoto :loop:foundendlocal&set %valname%=%last%exit /b 0



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like