Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Batch file SHIFT command [duplicate]

Tags:

batch-file

cmd

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.

like image 536
naveencgr8 Avatar asked Apr 08 '13 17:04

naveencgr8


People also ask

What does shift do in a batch file?

The shift command changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one—the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This is useful for writing a batch file that performs the same operation on any number of parameters.

How do you repeat a batch code?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What is %1 in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


1 Answers

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
like image 79
rojo Avatar answered Sep 19 '22 20:09

rojo