batch parameters: everything after %1

前端 未结 7 1086
离开以前
离开以前 2021-02-03 18:44

Duplicate:

  • Is there a way to indicate the last n parameters in a batch file?
  • how to get batch file parameters from Nth position on?
相关标签:
7条回答
  • 2021-02-03 18:47

    You can use SHIFT for this. It removes %1 and shifts all other arguments one lower. This script outputs all the arguments after %2 (so it outputs %3, %4...) until one of them is empty (so it's the last one):

    @echo off
    
    SHIFT
    SHIFT
    
    :loop
    if "%1" == "" goto end
    echo %1
    SHIFT
    goto loop
    
    :end
    

    EDIT: Removed example using %* as this doesn't work - %* always outputs all of the parameters

    0 讨论(0)
  • 2021-02-03 18:47

    I am not sure if there is a direct command but you can always use a simple loop and shift to get the result in a variable. Something like:

    @echo off
    set RESTVAR=
    shift
    :loop1
    if "%1"=="" goto after_loop
    set RESTVAR=%RESTVAR% %1
    shift
    goto loop1
    
    :after_loop
    echo %RESTVAR%
    
    

    Let me know if it helps!

    0 讨论(0)
  • 2021-02-03 18:47

    There is a shorter solution (one-liner) utilizing the tokenization capabilities of for loops:

    :: all_but_first.bat
    echo all: %*
    for /f "tokens=1,* delims= " %%a in ("%*") do set ALL_BUT_FIRST=%%b
    echo all but first: %ALL_BUT_FIRST%
    

    output:

    > all_but_first.bat foo bar baz
    all: foo bar baz
    all but first: bar baz
    
    0 讨论(0)
  • 2021-02-03 18:58

    The following will work for args with ", =, ' '. Based on Dmitry Sokolov answer. Fixed issue when second arg is the same as first arg.

    @echo off
    echo %*
    set _tail=%*
    call set _tail=%%_tail:*%1=%%
    echo %_tail%
    
    0 讨论(0)
  • 2021-02-03 19:03

    Sebi, here's the Syntax! There is a behavior, batch eating the equal signs which is not double quoted, it cause trouble in the scripts above. If you wan't to skip, i've made a modification, based on Raman Zhylich answer and strlen.cmd:

    @ECHO OFF
    SETLOCAL enableDelayedExpansion
    
    SET _tail=%*
    SET "_input="
    SET /A _len=0
    
    :again
    SET "_param=%1"
    SET "_input=%_input%%1"
    FOR /L %%i in (0,1,8191) DO IF "!_param:~%%i,1!"=="" (
        REM skip param
        SET /A _len+=%%i
        REM _len can't be use in substring
        FOR /L %%j in (!_len!,1,!_len!) DO (
            REM skip param separator
            SET /A _len+=1
            IF "!_tail:~%%j,1!"=="=" (SET "_input=%_input%=" & SHIFT & goto :again)
        )
    ) & goto :next
    :next
    IF %_len% NEQ 0 SET _tail=!_tail:~%_len%!
    
    ENDLOCAL & SET "_input=%_input%" & SET "_tail=%_tail%"
    
    0 讨论(0)
  • 2021-02-03 19:10

    The following will work for args with ", =, ' ' (as compared to @MaxTruxa answer)

    echo %*
    set _all=%*
    call set _tail=%%_all:*%2=%%
    set _tail=%2%_tail%
    echo %_tail%
    

    Test

    > get_tail.cmd "first 1" --flag="other options" --verbose
    "first 1" --flag="other options" --verbose
    --flag="other options" --verbose
    
    0 讨论(0)
提交回复
热议问题