How to make SHIFT work with %* in batch files

后端 未结 7 1838
独厮守ぢ
独厮守ぢ 2020-12-24 11:38

In my batch file on Windows XP, I want to use %* to expand to all parameters except the first.
Test file (foo.bat):



        
7条回答
  •  -上瘾入骨i
    2020-12-24 12:09

    Yet another obnoxious shortcoming of DOS/Windows batch programming...

    Not sure if this is actually better than some of the other answers here but thought I'd share something that seems to be working for me. This solution uses FOR loops rather than goto, and is contained in a reusable batch script.

    Separate batch script, "shiftn.bat":

    @echo off
    setlocal EnableDelayedExpansion
    set SHIFTN=%1
    FOR %%i IN (%*) DO IF !SHIFTN! GEQ 0 ( set /a SHIFTN=!SHIFTN! - 1 ) ELSE ( set SHIFTEDARGS=!SHIFTEDARGS! %%i ) 
    IF "%SHIFTEDARGS%" NEQ "" echo %SHIFTEDARGS:~1%
    

    How to use shiftn.bat in another batch script; in this example getting all arguments following the first (skipped) arg:

    FOR /f "usebackq delims=" %%i IN (`call shiftn.bat 1 %*`) DO set SHIFTEDARGS=%%i 
    

    Perhaps someone else can make use of some aspects of this solution (or offer suggestions for improvement).

提交回复
热议问题