Batch-Script - Iterate through arguments

前端 未结 6 602
暖寄归人
暖寄归人 2020-12-08 18:49

I have a batch-script with multiple arguments. I am reading the total count of them and then run a for loop like this:

@echo off
setlocal enabledelayedexpans         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 19:31

    @echo off
    setlocal enabledelayedexpansion
    
    set argCount=0
    for %%x in (%*) do (
       set /A argCount+=1
       set "argVec[!argCount!]=%%~x"
    )
    
    echo Number of processed arguments: %argCount%
    
    for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"
    

    For example:

    C:> test One "This is | the & second one" Third
    Number of processed arguments: 3
    1- "One"
    2- "This is | the & second one"
    3- "Third"
    

    Another one:

    C:> test One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve etc...
    Number of processed arguments: 13
    1- "One"
    2- "Two"
    3- "Three"
    4- "Four"
    5- "Five"
    6- "Six"
    7- "Seven"
    8- "Eight"
    9- "Nine"
    10- "Ten"
    11- "Eleven"
    12- "Twelve"
    13- "etc..."
    

提交回复
热议问题