如何更换使用命令行参数字符串一个bat文件中的字符串 [英] How to replace string inside a bat file with command line parameter string

查看:139
本文介绍了如何更换使用命令行参数字符串一个bat文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个cmd批处理文件如下:

I have following in a cmd batch file:

for /f %%l in (%2) do (for %%f in (%%l) do copy "%%f" %1))

请注意:这个剧本基本上都被读取包含其路径是%2给出分号隔开txt文件的文本文件(例如,其中包含C:\\ test1的\\ file1.cs; D:\\ file2.js)和复制文件到目标文件夹%1指定。

note : this script basically does is read a text file that contains semicolon delimited txt file whose path is given by %2(eg which contains c:\test1\file1.cs;d:\file2.js) and copy the files to destination folder specified by %1.

我需要更换%1 参数的字符串值x (这也将传递到批处理文件如: 也作为参数传递到批处理文件%3 )使用%4 值。

I need to replace the %1 parameter's string value of x (which is also passed to batch file e.g. %3) with %4 value which is also passed as parameter to batch file.

例如:

if %1 = 'test replace x with y'
%3=x
%4=y

所以输出应该是测试与代替Y

so the output should be 'test replace y with y'

我怎样才能做到这一点使用Windows CMD批间preTER?

How can I achieve this using Windows CMD batch interpreter?

推荐答案

首先,你必须%1 存储到一个变量,那么你将能够进行更换。

First of all, you'll have to store %1 into a variable, then you will be able to perform the replacements.

基本上,用于更换的语法是:

Basically, the syntax for the replacement is this:

%variable:str1=str2%

这意味着:替换每个 STR1 变量 STR2

在您的情况下,两个 STR1 STR2 的参数,而不是文字字符串。使用上面的模板直接和您可能会与此前pression结束:

In your case both str1 and str2 are parameters, not literal strings. Using the above template directly you might end up with this expression:

%变量:%= 3%4%

但是,这会混淆解析器,因为它不会知道 3%%4 应首先评估。事实上,它会先尝试评估%变量:%(和失败)

But that would confuse the parser, as it wouldn't know that %3 and %4 should be evaluated first. In fact, it would first try to evaluate %variable:% (and fail).

一个在此情况下,解决方案可以是使用一个称为方法的懒惰延迟评价的。基本上,你在哪里,你正在评估一个变量,以CALL命令传递命令。原始命令与其通话版'的转变是像这样:

One of the solutions in this case could be to use a method called 'lazy' delayed evaluation. Basically, you are passing the command where you are evaluating a variable, to the CALL command. The transformation of the original command to its 'CALL version' is like so:

ECHO%VAR% ==> CALL ECHO %% VAR %%

请注意双秒。在分析时,他们进行评估,以单一的秒。由此产生的命令将再次通过CALL解析,并最终效果是一样的原始命令, ECHO%VAR%的情况。

Note the double %s. At parse time they are evaluated to single %s. The resulting command would be parsed again by CALL, and the ultimate effect would be the same as in case of the original command, ECHO %var%.

所以它的工作原理一样的原始命令(这是好的),而我们正在赢得这里的评估以后的,我指的是最终评价,当变量实际上取代与它的价值。了解关于这种效果,我们可以构建我们的前pression以这样一种方式,%3 %4 首先评估,那么整个结果前pression。具体地讲,像这样的:

So it works the same as the original command (which is good), and what we are gaining here is the later time of evaluation, I mean the final evaluation, when the variable is actually replaced with its value. Knowing about that effect, we can construct our expression in such a way that %3 and %4 are evaluated first, and then the entire resulting expression. Specifically, like this:

%%variable:%3=%4%%

第一解析这个前pression会变成这样的事情之后:

After the first parse this expression would become something like this:

%variable:x=y%

这将重新分析,输出将变量的修改内容。

That would be parsed again, and the output would be variable's modified contents.

为了更好的说明,这里有一个简单的例子:

For better illustration, here's a simple working example:

SET "output=%1"
CALL SET output=%%output:%3=%4%%
ECHO %output%


更新

有在做同样的事情,而我也许应该已经首次提及的另一种方法。

There's another method of doing the same thing, which I should probably have mentioned first.

Windows命令外壳支持的正确的延迟扩展。它是利用简单,但是有一些注意事项。

The Windows command shell supports a proper delayed expansion. It is simpler in use, but has some caveats.

首先,如何使用它。对于延迟扩展的语法是!无功!而不是%VAR%立即展开(这仍然有效,可与延迟扩展语法一起使用)。

First, how to use it. The syntax for delayed expansion is !var! instead of %var% for immediate expansion (which remains valid and can be used alongside with the delayed expansion syntax).

也许 VAR ,直到你能够与命令的语法不会在你的脚本工作:!

Probably !var! will not work in your script until you enable the syntax with the command:

SETLOCAL EnableDelayedExpansion

ENDLOCAL 命令关闭其内延迟扩展语法是通过命令shell PTED有效和跨$ P $块。

The ENDLOCAL command closes the block within which the delayed expansion syntax is valid and interpreted by the command shell.

以上示例脚本,可以改写如下:

The above example script could be rewritten like this:

SET "output=%1"
SETLOCAL EnableDelayedExpansion
SET output=!output:%3=%4!
ECHO !output!
ENDLOCAL

所以,这是如何工作的设置输出的情况下,输出=!%3 =%4 命令:


  • %3 %4 在分析时立即进行评估,即 - 它们被替换与 X 分别为;

  • %3 and %4 are evaluated immediately, i.e. at the parse time – they are replaced with x and y respectively;

命令变成这样: SET输出=输出:X = Y ;

该命令是要执行! - 在 前pression评估( X s的替换 S);

the command is about to execute – the ! expression is evaluated (xs are replaced with ys);

执行该命令 - 在输出变量修改

the command is executed – the output variable is modified.

现在有关注意事项。首先要记住的一点是,成为语法的一部分,并且被消耗和跨$ P $每当遇到PTED。所以你需要逃避它,你想用它作为文字(如 ^!)。

Now about the caveats. The first thing to remember is that the ! becomes part of the syntax and is consumed and interpreted whenever encountered. So you'll need to escape it where you want to use it as a literal (like ^!).

另外一个需要注意的是一个SETLOCAL / ENDLOCAL块的主要影响。事实是,所有这些块内更改环境变量,那么,本地的。在退出块(在执行 ENDLOCAL )的变量设置为它的值为之前进入它(之前执行 SETLOCAL )。这意味着你输出的更改值将只有你不得不开始为 SETLOCAL 块内有效使用摆在首位的延迟扩展。也许这可能不是你的具体情况有问题,如果你只需要修改的值,然后使用它了,但你可能应该记住它的未来。

Another caveat is the primary effect of a SETLOCAL/ENDLOCAL block. The thing is, all the changes to environment variables within such a block are, well, local. Upon exiting the block (upon executing ENDLOCAL) the variable is set to the value it had prior to entering it (prior to executing SETLOCAL). This means for you that the changed value of output will only be valid within the SETLOCAL block which you had to initiate for using the delayed expansion in the first place. Possibly this may not be a problem in your particular case, if you just need to modify the value and then use it right away, but you should probably have to remember it for the future.

注:按的杰布的的评论,你可以保存修改后的值,并使用这一招离开SETLOCAL块:

Note: As per jeb's comment, you can save the modified value and leave the SETLOCAL block using this trick:

ENDLOCAL & SET "output=%output%"

&安培; 操作人员只需划命令时,它们被放置在同一行。它们被执行一前一后,在它们被指定的相同顺序。关键是,通过分析该行的时刻,SETLOCAL块尚未离开呢,所以%输出%评估修改后的价值,这是仍然有效。但实际上在执行任务的之后 ENDLOCAL ,即离开块之后。所以你有效地存储离开块,从而$ ​​P $ pserving更改后的修正值。 (感谢,​​杰布!)

The & operator simply delimits the commands when they are placed on the same line. They are executed one after the other, in the same order they are specified. The thing is, by the moment of parsing the line, the SETLOCAL block hasn't been left yet, so %output% evaluates to the modified value, which is still valid. But the assignment is actually executed after ENDLOCAL, i.e. after leaving the block. So you are effectively storing the modified value after leaving the block, thus preserving the changes. (Thanks, jeb!)

更多信息:


  1. 在延迟扩展:

  1. On delayed expansion:


  • <一个href=\"http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx\">http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx

<一个href=\"http://www.dostips.com/DtTutoFramework.php#_Toc128587174\">http://www.dostips.com/DtTutoFramework.php#_Toc128587174

在更换子串:

这篇关于如何更换使用命令行参数字符串一个bat文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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