17

I'm trying to display up to 9 parameters across the screen, and then display one fewer on each following line, until there are none left.

I tried this:

@echo off
echo %*
shift
echo %*
shift
echo %*

Actual Result:

   a b c d e f
   a b c d e f

Expected Result:

A B C D E F
B C D E F
C D E F
D E F
E F
F

Any help?

Thanks.

naveencgr8
  • 331
  • 1
  • 4
  • 13
  • @david Thanks for the instructions, I get it now. Thanks a lot. ECHO %1 %2 %3 %4 %5 %6 %7 %8 %9 shift ECHO %1 %2 %3 %4 %5 %6 %7 %8 %9 shift ECHO %1 %2 %3 %4 %5 %6 %7 %8 %9 – naveencgr8 Apr 08 '13 at 17:42
  • a nice "catch all" example: http://stackoverflow.com/questions/830565/how-do-i-check-that-a-parameter-is-defined-when-calling-a-batch-file/34552964#34552964 –  Jan 01 '16 at 01:49

4 Answers4

15

SHIFT is worthwhile if you want to get the value of %1, %2, etc. It doesn't affect %*. This script gives you the output you expect.

@echo off
setlocal enabledelayedexpansion

set args=0
for %%I in (%*) do set /a "args+=1"
for /l %%I in (1,1,%args%) do (
    set /a idx=0
    for %%a in (%*) do (
        set /a "idx+=1"
        if !idx! geq %%I set /p "=%%a "<NUL
    )
    echo;
)

Output:

C:\Users\me\Desktop>test a b c d e f
a b c d e f
b c d e f
c d e f
d e f
e f
f
rojo
  • 24,000
  • 5
  • 55
  • 101
11

Shift doesn't change the actual order, just the index/pointer into the arguments.

Try this:

@echo off
echo %1
shift 
echo %1
shift
echo %1
echo %*

And you get this:

a
b
c
a b c d
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
4

"Shift has no affect on the %* batch parameter"

Source: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

I suggest altering your code to a loop or something

David Starkey
  • 1,840
  • 3
  • 32
  • 48
3

The %* always prints all arguments from the command line (except %0). It does not honor the SHIFT command.

You need to explictly echo all arguments by position: %1 %2 %3...

@echo off
:loop
if "%~1" neq "" (
  echo %1 %2 %3 %4 %5 %6 %7 %8 %9
  shift
  goto :loop
)
dbenham
  • 127,446
  • 28
  • 251
  • 390