如何使在批处理文件%*轮班工作 [英] How to make SHIFT work with %* in batch files

查看:140
本文介绍了如何使在批处理文件%*轮班工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows XP上我的批处理文件,我想用%* 扩大到所有的参数除了第一。
测试文件 foo.bat 的):

In my batch file on Windows XP, I want to use %* to expand to all parameters except the first.
Test file (foo.bat):

@echo off
echo %*
shift
echo %*

电话:

C:\> foo a b c d e f

实际结果:

a b c d e f
a b c d e f

期望结果:

a b c d e f
b c d e f

我怎样才能达到预期的效果?谢谢!

How can I achieve the desired result? Thanks!!

推荐答案

那岂不是美好的,如果CMD.EXE的工作这样!不幸的是,不是一个好的语法,将你想要做什么。你能做的最好的就是你自己解析命令行,并建立一个新的参数列表。

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.

参数处理是Windows批处理编程的许多薄弱环节之一。对于几乎所有的解决方案,存在导致问题的一个例外。

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

这篇关于如何使在批处理文件%*轮班工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆