有没有一种方法来传递参数"按名称" (而不是顺序)到批处理.bat文件? [英] Is there a way to pass parameters "by name" (and not by order) to a batch .bat file?

查看:126
本文介绍了有没有一种方法来传递参数"按名称" (而不是顺序)到批处理.bat文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够传递参数给一个Windows批处理文件名(而不是顺序)。我的目的是为了给最终用户的灵活性,通过以任意顺序参数,批处理文件应该还是可以对其进行处理。

I need to be able to pass parameters to a windows batch file BY NAME (and NOT by order). My purpose here is to give end user the flexibility to pass parameters in any order, and the batch file should still be able to process them.

一个例子让我更清楚的问题:

An example to make my question clearer:

在命令行中,用户执行以下操作:
somebatchfile.bat originalFile.txt newFile.txt

in the command line, user does the following: somebatchfile.bat originalFile.txt newFile.txt

somebatchfile.bat 有一个简单的语句(第一个参数%1%)来复制原始文件的内容,新的文件(第二个参数%2%) 。它可以像下面的语句一样简单:
复制%1%%2%

Inside somebatchfile.bat there is a simple statement to copy the contents of original file (first parameter %1%) to the new file (second parameter %2%). It could be as simple as the following statement: copy %1% %2%

现在,如果用户通过以相反的顺序上面的参数,其结果将是远离理想(其实非常错误的)。

Now, if user passes the above parameters in reverse order, the result will be far from desirable (very WRONG in fact).

那么,有没有用户通过名字来传递参数的方式:例如 somebatchfile.bat源程序= originalFile.txtTARGET = newFile.txt和脚本来识别它们,并在use'em正确的位置如复制%SOURCE%%的目标%

So, is there a way for user to pass parameters by name: e.g. somebatchfile.bat "SOURC=originalFile.txt" "TARGET=newFile.txt" and for script to recognize them and use'em in correct places e.g. copy %SOURCE% %TARGET%?

谢谢,

推荐答案

是的,你可以做这样的事情,虽然我不认为你可以使用=作为标记分隔符。你可以使用说冒号: somebatchfile.bat源程序:originalFile.txtTARGET:newFile.txt。下面是您可能如何分割标记的例子:

Yeah you could do something like that though I don't think you can use "=" as a token delimiter. You could use say a colon ":", somebatchfile.bat "SOURC:originalFile.txt" "TARGET:newFile.txt". Here is an example of how you might split the tokens:

@echo off

set foo=%1
echo input: %foo%

for /f "tokens=1,2 delims=:" %%a in ("%foo%") do set name=%%a & set val=%%b

echo name:  %name%
echo value: %val%

运行,这将产生这样的:

Running this would produce this:

C:\>test.bat SOURC:originalFile.txt
input: SOURC:originalFile.txt
name:  SOURC
value: originalFile.txt

好吧,也许是太接近睡觉时间,我昨晚但今天上午又找,你可以这样做:

Ok, maybe it was too close to bed time for me last night but looking again this morning, you can do this:

@echo off

set %1
set %2

echo source: %SOURCE%
echo target: %TARGET%

这将产生这样(请注意,我颠倒了命令行的源和目标,以表明他们是正确设置和检索):

Which would produce this (note that I reversed the source and target on the command line to show they are set and retrieved correctly):

C:\>test.bat "TARGET=newFile.txt" "SOURCE=originalFile.txt"
source: originalFile.txt
target: newFile.txt

请注意,1%和2%,之前评估的设置所以这些确实会设置环境变量。他们的必须但是可以在命令行上引用。

Note that %1 and %2 are evaluated before the set so these do get set as environment variables. They must however be quoted on the command line.

这篇关于有没有一种方法来传递参数"按名称" (而不是顺序)到批处理.bat文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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