How to pass more than 9 parameters to batch file

后端 未结 11 1464
花落未央
花落未央 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:36

    You can use the shift command which shifts the arguments back 1 step, for example %9 would be stored in %8 and %8 would be stored in %7 etc, so if that happened there would be room for a 10th argument in the 9th argument if that makes sense, here is an example:

    @echo off
    set arg1=%~1
    set arg2=%~2
    set arg3=%~3
    set arg4=%~4
    set arg5=%~5
    set arg6=%~6
    set arg7=%~7
    set arg8=%~8
    set arg9=%~9
    shift
    set arg10=%~9
    shift
    set arg11=%~9
    shift
    set arg12=%~9
    

    and instead of using %~1 use %arg1% etc, and to use the 10th, 11th, 12th argument you will use %arg10%, %arg11%, %arg12%.

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

    My alternative allows you skip any number of arguments and requires no shifting

    SETLOCAL
      SET /a SKIP=3
      CALL :SkipAndExec SKIP THESE VARS echo numbers: 1 2 3 4 5 6 7 8 9 0 11 12 13 14
    ENDLOCAL
    GOTO:eof
    
    :SkipAndExec
    SETLOCAL
    SET /a n=0
    FOR %arg IN (%*) DO (
      SET /a n=n+1
      IF %n% GT %SKIP% (
        :: here you could compose a string to use later
        SET _CMD_=%_CMD_% %arg%
      )
    )
    :: here you could call another function and pass in %_CMD_%
    CALL %_CMD_%
    GOTO:eof
    

    If you execute that script, it will print:

    numbers: 1 2 3 4 5 6 7 8 9 0 11 12 13 14
    
    0 讨论(0)
  • 2020-12-17 18:43

    Dealing with * in parameters examples:

    @ECHO OFF
    SETLOCAL DISABLEDELAYEDEXPANSION
    
    CALL testLIMIT.bat 3 4 2 1 2 3 4 5 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 A Star * is Born twice * now
    
    ECHO HOME
    Pause
    exit
    
    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
    REM :: Used to define the Array Index values and Range
    set _I=0
    
    REM :: The Core piece in recieving Large numbers of Parameters.
    REM :: Processes all Parameters. Tested to over 2500 Parameters.
    
    Set StarTest=0
    FOR %%a in (%*) DO (
        IF EXIST "%%~a" Call :StarTest
        IF NOT EXIST "%%~a" (
        CALL Set /a _I+=1
        CALL Set "arg[!_I!]=%%a"
        CALL Set StarTest=0
        )
    )
    
    
    REM :: Loops through the Array to display Parameter Values.
    
    FOR /L %%a in (1,1,!_I!) DO (
    ECHO !arg[%%a]!
    )
    
    echo %_I% Arguments received.
    Pause
    GOTO :EOF
    
    :StarTest
    IF %StarTest% GTR 0 GOTO :EOF
    Set /a StarTest+=1
    Set /a _I+=1
    Set "arg[!_I!]=*"
    GOTO :EOF
    

    Processing parameters with Shift is Entirely Unneccesary, not to mention messy.

    The following approach, Similar to Marcels Answer, will process Exceptionally large numbers of parameters without any requirement to know how many Arguments will be recieved.

    It uses A basic For loop with the * wildcard to process all tokens and Assigns them to an Array, also giving a Value to how many arguments the File recieved to enable processing.

    
    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
    REM :: Used to define the Array Index values and Range
    set _I=0
    
    REM :: The Core piece in recieving Large numbers of Parameters.
    REM :: Processes all Parameters. Tested to over 2500 Parameters.
    
    FOR %%a in (%*) DO (
    Set /a _I+=1
    Set "arg[!_I!]=%%a"
    )
    
    REM :: Loops through the Array to display Parameter Values. Modify as needed to Process Parameters.
    
    FOR /L %%a in (1,1,!_I!) DO (
    ECHO !arg[%%a]!
    )
    
    echo %_I% Arguments received.
    Pause
    GOTO :EOF
    
    
    0 讨论(0)
  • 2020-12-17 18:44

    Use %*. Command Line arguments (Parameters)

    People who looking for how to passthrough any number of params to an executable:

    @ECHO OFF
    SET "EXECUTABLE=C:\PATH\TO\MY\EXECUTABLE.EXE"
    CALL %EXECUTABLE% %*
    

    Answering the question (openurl.bat):

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
    SET URL=
    SET /A BASECONCATENATED=0
    FOR %%A IN (%*) DO (
        SET PARAM=%%A
        SET PARAM=!PARAM:"=!
    
        IF !BASECONCATENATED! EQU 0 (
            SET "URL=!URL!!PARAM!^?"
            SET /A BASECONCATENATED=1
        ) ELSE (
            SET "URL=!URL!!PARAM!^&"
        ) 
    )
    
    SET URL=!URL:~0,-1!
    
    CALL "%SYSTEMDRIVE%\Program Files\Internet Explorer\iexplore.exe" "!URL!"
    
    REM EXAMPLE cmd.exe "openurl.bat" http://www.example.com/addPerson.php "firstName=Juan" "lastName=Cerezo" "hobby1=Coding" "hobby2=Learning" "hobby3=Play Videogames"
    
    0 讨论(0)
  • 2020-12-17 18:46

    The solution by basin should work. To address the problem with the code in the question: You can't just put multiple commands in a single line and you can't use the shift command in the middle of another command's arguments. Basically you have to split everything up, like shown in the other answer. To make it more concise and readable I would do it like this:

    set "ieargs=http://example.com?firstName=%1^&middleName=%2^&lastName=%3^&country=%4^&address=%5^&address2=%6^&address3=%7^&mobileNo=%8^&landlineNo=%9"
    
    @for /L %%i in (0,1,8) do shift
    set "ieargs=%ieargs%^&emailAddress=%1^&hobby1=%2^&hobby2=%3^&hobby3=%4^&hobby4=%5^&hobby5=%6"
    
    start iexplore %ieargs%
    

    The first nine arguments are saved in the string. Then it loops and shifts nine times, so that %1 will be the thenth argument.

    If you need even more arguments you can just repeat this as often as you need:

    set "ieargs=http://example.com?firstName=%1^&middleName=%2^&lastName=%3^&country=%4^&address=%5^&address2=%6^&address3=%7^&mobileNo=%8^&landlineNo=%9"
    
    @for /L %%i in (0,1,8) do shift
    set "ieargs=%ieargs%^&emailAddress=%1^&hobby1=%2^&hobby2=%3^&hobby3=%4^&hobby4=%5^&hobby5=%6^&hobby6=%7^&hobby7=%8^&hobby8=%9"
    
    @for /L %%i in (0,1,8) do shift
    set "ieargs=%ieargs%^&hobby9=%1^&hobby10=%2^&hobby11=%3^&hobby12=%4"
    
    start iexplore %ieargs%
    
    0 讨论(0)
  • 2020-12-17 18:48
    @echo off
    set /a a=0
    :9
    set CustomVar%a%=%1
    @shift
    set /a a=%a%+1
    if %a%==300 goto :8
    goto :9
    :8
    @echo on
    
    0 讨论(0)
提交回复
热议问题