在`shift`之后使用`%*`

jez*_*jez 5 windows batch-file

据我所知,在Windows批处理文件,%*扩展到所有的命令行参数,并且shift将这个编号的命令行参数%1,%2等等.但是,它并没有改变的内容%*.

我该怎么办,如果我想有一个版本%*没有反映的效果shift?我明白我可以%1 %2 %3 %4 %5 %6 %7 %8 %9在转移之后说出来,但这似乎是愚蠢而且有潜在危险,这限制了我固定数量的论点.

虽然这不是特定于python的问题,但我可能有必要理解我想要这种行为的原因是我必须编写一个SelectPython.bat预先配置某些环境变量的批处理文件 ,以便导航不同Python的babel我有的发行版(你必须设置%PYTHONHOME%,%PYTHONPATH%并且%PATH%在某种程度上你可以调用Python二进制文件并确信你会获得正确的发行版).我当前的脚本适用于设置这些变量,但我希望能够在一行中调用它 Python - 例如:

SelectPython C:\Python35  pythonw.exe myscript.py arg1 arg2 arg3 ...
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望我的批处理文件用于shift"吃掉"第一个参数,相应地处理它并设置环境,然后自动链式执行由其余参数形成的字符串.该原理类似于env在posix系统中包装命令的方式:

env FOO=1 echo $FOO     # wrap the `echo` command to be executed in the context of specified environment settings
Run Code Online (Sandbox Code Playgroud)

到目前为止我有这个 - 最后一行是问题所在:

@echo off
set "LOC=%CD%
if not "%~1" == "" set "LOC=%~1
if exist "%LOC%\python.exe" goto :Success

echo "python.exe not found in %LOC%"
goto :eof

:Success
:: Canonicalize the resulting path:
pushd %LOC%
set "LOC=%CD%
popd

:: Let Python know where its own files are:
set "PYTHONHOME=%LOC%
set "PYTHONPATH=%LOC%;%LOC%\Lib\site-packages

:: Put Python's location at the beginning of the system path if it's not there already:
echo "%PATH%" | findstr /i /b /c:"%PYTHONHOME%" > nul || set "PATH=%PYTHONHOME%;%PYTHONHOME%\Scripts;%PATH%

:: Now execute the rest:
shift
if "%~1" == "" goto :eof
%1 %2 %3 %4 %5 %6 %7 %8 %9
:: This is unsatsifactory - what if there are more than 9 arguments?
Run Code Online (Sandbox Code Playgroud)

更新: 感谢Stephan我的工作解决方案现在有以下更改的结束部分:

:: Now execute the rest of the arguments, if any:
shift
if @%1 == @ goto :eof
set command=
:BuildCommand
if @%1 == @ goto :CommandFinished
set "command=%command% %1"
shift
goto :BuildCommand
:CommandFinished
%command%
Run Code Online (Sandbox Code Playgroud)

Ste*_*han 4

构建你自己的“%*”(我命名它%params%):

set "params="
:build
if @%1==@ goto :cont
shift
set "params=%params% %1"
goto :build
:cont
echo params are %params%
Run Code Online (Sandbox Code Playgroud)