mat*_*kie 15 windows batch-file
继续如何在批处理文件中传递命令行参数如何通过完全指定参数来获取其余参数?我不想使用SHIFT,因为我不知道可能有多少参数,并且如果可以的话,我们希望避免对它们进行计数.
例如,给定此批处理文件:
@echo off
set par1=%1
set par2=%2
set par3=%3
set therest=%???
echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%
Run Code Online (Sandbox Code Playgroud)
跑步mybatch opt1 opt2 opt3 opt4 opt5 ...opt20
会产生:
the script is mybatch
Parameter 1 is opt1
Parameter 2 is opt2
Parameter 3 is opt3
and the rest are opt4 opt5 ...opt20
Run Code Online (Sandbox Code Playgroud)
我知道%*
给出所有参数,但我不是前三个(例如).
Pat*_*uff 23
以下是如何在不使用的情况下执行此操作SHIFT
:
@echo off
for /f "tokens=1-3*" %%a in ("%*") do (
set par1=%%a
set par2=%%b
set par3=%%c
set therest=%%d
)
echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%
Run Code Online (Sandbox Code Playgroud)
@echo off
setlocal enabledelayedexpansion
set therest=;;;;;%*
set therest=!therest:;;;;;%1 %2 %3 =!
echo the script is %0
echo Parameter 1 is %1
echo Parameter 2 is %2
echo Parameter 3 is %3
echo and the rest are: %therest%
Run Code Online (Sandbox Code Playgroud)
这适用于带引号的参数以及具有等号或逗号的参数,只要前三个参数没有这些特殊的分隔符即可。
示例输出:
test_args.bat "1 1 1" 2 3 --a=b "x y z"
Parameter 1 is "1 1 1"
Parameter 2 is 2
Parameter 3 is 3
and the rest are: --a=b "x y z"
Run Code Online (Sandbox Code Playgroud)
这可以通过替换%1 %2 %3
原始命令行来实现%*
。前五个分号只是为了确保仅%1 %2 %3
替换第一次出现的分号。
归档时间: |
|
查看次数: |
27043 次 |
最近记录: |