How to pass more than 9 parameters to batch file

后端 未结 11 1465
花落未央
花落未央 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
    
    0 讨论(0)
  • 2020-12-17 18:52
    :: set PARAMn and count parameters
    SET /a paramcount=1
    :paramloop
    SET "param%paramcount%=%~1"
    IF DEFINED param%paramcount% SET /a paramcount+=1&shift&GOTO paramloop
    SET /a paramcount -=1
    

    This routine should set param1..paramn for you, with a count.

    Unfortunately, your posted code appears indecipherable, so actually using the values - that's up to you.

    0 讨论(0)
  • 2020-12-17 18:53

    You could make it less error prone by adding one parameter at a time. This way, if you have to add a new one in the middle or take one parameter out, you don't need to renumber the rest: it will just be a two line change. Hopefully there aren't spaces in your parameters but that is another story.

    set v=http://example.com
    rem first parameter is ?: subsequent ones are &
    set sep=?
    call :add firstName %1
    shift
    call :add middleName %1
    shift
    call :add lastName %1
    shift
    ...
    start iexplore "%v%"
    goto :eof
    :add
        rem add a parameter
        set v=%v%%sep%%1=%2
        set sep=&
        goto :eof
    
    0 讨论(0)
  • 2020-12-17 18:56

    save the first nine args in a variable. THEN call shift multiple times and only then use the rest:

    set "v=http://example.com?firstName=%1&middleName=%2&lastName=%3&country=%4&address=%5&address2=%6&address3=%7&mobileNo=%8&landlineNo=%9"
    shift
    shift
    shift
    shift
    shift
    shift
    shift
    shift
    shift
    start iexplore %v%&emailAddress=%1&hobby1=%2&hobby2=%3&hobby3=%4&hobby4=%5&hobby5=%6
    
    0 讨论(0)
  • 2020-12-17 19:01

    Here's an even easier solution that assumes you merely want to pass the values to another program. This example allows passing up to 27 parameters to zip...

    @echo OFF
    set ZipArgs=%1 %2 %3 %4 %5 %6 %7 %8 %9
    for /L %%i in (0,1,8) do @shift
    set ZipArgs=%ZipArgs% %1 %2 %3 %4 %5 %6 %7 %8 %9
    for /L %%i in (0,1,8) do @shift
    set ZipArgs=%ZipArgs% %1 %2 %3 %4 %5 %6 %7 %8 %9
    "C:\Program Files\WinZip\wzzip.exe" %ZipArgs%
    

    The first line merely turns off echoing each line to the screen. The lines that start with set ZipArgs build the environment variable ZipArgs with the next 9 parameters from the command line. The "for" lines use the SHIFT command to discard the first 9 parameters bringing the next 9 "up front". The last line executes zip passing to it the assembled parameter values.

    You could complicate things by adding code to check for the number of parameters on the command line and act appropriately, but this does the trick just fine for me.

    0 讨论(0)
提交回复
热议问题