加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 如何从第N个位置获取批处理文件参数?

发布时间:2020-12-13 20:18:19 所属栏目:Windows 来源:网络整理
导读:进一步到 How to Pass Command Line Parameters in batch file如何通过指定它们得到其余的参数?我不想使用SHIFT,因为我不知道可能有多少个参数,并且想避免计数它们,如果可以的话. 例如,给定此批处理文件: @echo offset par1=%1set par2=%2set par3=%3set t
进一步到 How to Pass Command Line Parameters in 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%

运行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

我知道%*给出了所有的参数,但是我不会先前三个(例如).

不用使用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%

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读