Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Documentation SHIFT command

May
3,515
5
The first sentence of HELP -> SHIFT is:

"Purpose: Allows the use of more than 512 parameters in a batch file, or iterating through its parameters. This command can be used only in batch files."

In V05 the number was 256, since V06 the number is 512. In V08 and earlier versions the number matched the number of parameters directly accessible as %n. However, since V09 one can access up to 4096 parameters as %n. Shouldn't the number be updated?
 
Or the sentence rewritten to avoid the use of a number altogether.
 
Or the sentence rewritten to avoid the use of a number altogether.

Which will lead to the inevitable question... "How many parameters will SHIFT handle?"
 
Which will lead to the inevitable question... "How many parameters will SHIFT handle?"

And the answer is "all of them": more than you'll ever put on an actual command line.

(Does SHIFT have any use other than CMD compatibility?)
 
(Does SHIFT have any use other than CMD compatibility?)

I have no clue; I've never used it. I think the only time I ever used a SHIFT command was in a PERL script.
 
It depends on how you parse your command line arguments I suppose. You can loop until the %1 value is empty and SHIFT each time through the loop to get the next %1, or iterate over the count of arguments.
Code:
do until %1.=. 
    switch %1
    case A .OR. B
      echo %1
    caseall
      shift
    default
       echo usage: blah blah blah...
       quit
    endswitch
enddo
 
vs.
 
do i=1 to %#
   switch %[%i]
    case A .OR. B
      echo %[%i]
    default
       echo usage: blah blah blah...
       quit
    endswitch
enddo

-Scott
 
Does SHIFT have any use other than CMD compatibility?
Yes - if you do not want to use %[%n] everywhere instead of %1, and there may be multiple parameters requiring identical processing.
It depends on how you parse your command line arguments I suppose. You can loop until the %1 value is empty and SHIFT each time through the loop to get the next %1, or iterate over the count of arguments.

When my batch program is designed to perform identical processing on multiple parameters, I found the structure below the simplest:

do while %# GT 0
REM process current %1
shift
enddo

This works without evaluating each parameter just for loop control, and even if an intermediate one is an empty string (which your version, Scott, would not handle).

Of course there are more elegant methods in other programming languages, but this is TCC, which has some archaic limitations.
 
Just as an FYI, I use Steve's technique all the time. Yet another wonderful feature of TCC (although I suppose it "inherited" it from cmd.exe ;)) - Dan
 

Similar threads

Back
Top