Documentation SHIFT command

May 20, 2008
3,515
4
Elkridge, MD, USA
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?
 
Jan 19, 2011
614
15
Norman, OK
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?"
 
Jan 19, 2011
614
15
Norman, OK
(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.
 

samintz

Scott Mintz
May 20, 2008
1,555
26
Solon, OH, USA
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
 
May 20, 2008
3,515
4
Elkridge, MD, USA
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.
 
May 24, 2010
855
0
Northlake, Il
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