How to pass more than 9 parameters to batch file

后端 未结 11 1467
花落未央
花落未央 2020-12-17 18:08

How can I pass more than nine parameters to bach file.
I tried another SO question How do you utilize more than 9 arguments when calling a label in a CMD batch-script?

11条回答
  •  感情败类
    2020-12-17 18:50

    Here is a simple answer: shift is the key and then loop values and assign them into variables. setup counter to indicate which variable is each parameter.

    Example gets alphabets into batch file and sets them into arg1, arg2 etc variables.

    batchfile test.bat example:

    @echo off
    
    set /a counter=1
    :ARGLBEG
    if "%1"=="" goto ARGLEND
    set arg%counter%=%1
    goto ARGNEXT 
    
    :ARGNEXT
    shift
    set /a counter=%counter%+1
    goto ARGLBEG
    
    :ARGLEND
    echo "List values."
    set arg
    
    echo "Just one value, arg9."
    echo %arg9%
    goto end
    
    :end
    

    How to use test.bat and how results looks:

    test.bat A B c d e F G H i J k L M N O P Q R S t u v x y z
    

    Results.

    "List values."
    arg1=A
    arg10=J
    arg11=k
    arg12=L
    arg13=M
    arg14=N
    arg15=O
    arg16=P
    arg17=Q
    arg18=R
    arg19=S
    arg2=B
    arg20=t
    arg21=u
    arg22=v
    arg23=x
    arg24=y
    arg25=z
    arg3=c
    arg4=d
    arg5=e
    arg6=F
    arg7=G
    arg8=H
    arg9=i
    "Just one value, arg9."
    i
    

提交回复
热议问题