How to make SHIFT work with %* in batch files

后端 未结 7 1801
独厮守ぢ
独厮守ぢ 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条回答
  • 2020-12-24 12:14

    Wouldn't it be wonderful if CMD.EXE worked that way! Unfortunately there is not a good syntax that will do what you want. The best you can do is parse the command line yourself and build a new argument list.

    Something like this can work.

    @echo off
    setlocal
    echo %*
    shift
    set "args="
    :parse
    if "%~1" neq "" (
      set args=%args% %1
      shift
      goto :parse
    )
    if defined args set args=%args:~1%
    echo(%args%
    

    But the above has problems if an argument contains special characters like ^, &, >, <, | that were escaped instead of quoted.

    Argument handling is one of many weak aspects of Windows batch programming. For just about every solution, there exists an exception that causes problems.

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