1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Documentation SHIFT command

Discussion in 'Support' started by Steve Fabian, May 1, 2012.

  1. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    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?
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,374
    Likes Received:
    40
    Or the sentence rewritten to avoid the use of a number altogether.
     
  3. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    Which will lead to the inevitable question... "How many parameters will SHIFT handle?"
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,374
    Likes Received:
    40
    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?)
     
  5. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    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.
     
  6. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,203
    Likes Received:
    11
    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
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Yes - if you do not want to use %[%n] everywhere instead of %1, and there may be multiple parameters requiring identical processing.
    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.
     
  8. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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
     

Share This Page