如何从第N个位置的批处理文件的参数呢? [英] how to get batch file parameters from Nth position on?
问题描述
继<一个href=\"http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-in-batch-file\">How将命令行参数在批处理文件是怎么开始的其余参数与准确指定它们?我不想用SHIFT,因为我不知道有多少参数有可能是和想避免计数他们,如果我能。
Further to How to Pass Command Line Parameters in batch file how does one get the rest of the parameters with specifying them exactly? I don't want to use SHIFT because I don't know how many parameters there might be and would like to avoid counting them, if I can.
例如,鉴于此批处理文件:
For example, given this batch file:
@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%
运行 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
我知道%*
给出了所有的参数,但我不wan't前三(例如)。
I know %*
gives all the parameters, but I don't wan't the first three (for example).
推荐答案
下面是你如何能做到这一点,而无需使用 SHIFT
:
Here's how you can do it without using 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%
这篇关于如何从第N个位置的批处理文件的参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!