批处理脚本帮助-将DelayedExpansion Var的子字符串替换为另一个DelayedExpansion Var [英] Batch Scripting Help - Replace Substring of a DelayedExpansion Var with another DelayedExpansion Var

查看:97
本文介绍了批处理脚本帮助-将DelayedExpansion Var的子字符串替换为另一个DelayedExpansion Var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在尝试做!var1:SomeText =!var2 !!但是此代码不起作用. 我想念什么?

Basically I'm trying to do !var1:SomeText=!var2!! but this code doesn't work. What am I missing?

推荐答案

在执行使用变量进行搜索和/或替换的搜索和替换操作时,扩展顺序至关重要.必须先扩展内部变量,然后才能进行外部搜索和替换扩展.尝试对两者使用延迟扩展显然是行不通的,因为延迟扩展是在某个时间点发生的.

The order of expansion is critical when doing a search and replace operation that uses a variable for the search and/or the replace. The inner variable must be expanded before the outer search and replace expansion takes place. Trying to used delayed expansion for both obviously can't work because the delayed expansion occurs at one point in time.

在另一个变量中扩展变量的经典方法是使用延迟扩展进行外部扩展,而对内部进行常规扩展:echo !var1:SomeText=%var2%!"

The classic method for expansion of a variable within another variable uses delayed expansion for the outer, and normal for the inner: echo !var1:SomeText=%var2%!"

出于某种原因,我将假设您都想使用延迟扩展.扩展可能发生在代码块中,并且其中一个变量设置在同一代码块中.常规扩展无法正常工作,因为在该块结束之前,它看不到在该块内分配的值.

I am going to assume you wanted to use delayed expansion for both for a reason. Perhaps the expansion occurs within a block of code and one of the variables was set in the same block. Normal expansion won't work because it can't see the value that was assigned within the block until after the block concludes.

解决方案1 ​​

解决问题的一种方法是使用CALL:

One way to solve the problem is to use CALL:

call echo %%var1:SomeText=!var2!%% 

其工作原理如下:

  1. 解析器的百分比阶段将双精度百分比转换为单精度百分比,从而导致
    call echo %var1:SomeText=!var2!%

  1. The percent phase of the parser converts double percents into single percents, resulting in
    call echo %var1:SomeText=!var2!%

延迟的扩展会扩展!var2 !,从而导致
call echo %var1:SomeText=ReplacementText%

The delayed expansion expands !var2!, resulting in
call echo %var1:SomeText=ReplacementText%

将执行CALL ECHO,并执行附加级别的百分比处理.执行搜索和替换扩展,导致ResultOfSearchAndReplace被回显到屏幕.

The CALL ECHO is executed and an additional level of percent processing takes place. The search and replace expansion is executed, resulting in ResultOfSearchAndReplace being echoed to the screen.

这有效,但是相对较慢.如果扩展值具有特殊字符(例如>&|),也会出现问题.我很少使用这种技术.

This works, but it is relatively slow. It also can have problems if the expanded value has special characters like >, & or |. I rarely use this technique.

解决方案2

快速且可靠的方法是分两步进行扩展.首先将!var2!的值传输到FOR变量.然后,您可以将FOR变量用作替换字符串,并在第二步中使用延迟扩展.这完全避免了更脆弱的百分比扩展.

The fast and more reliable method is to do the expansion in two steps. First transfer the value of !var2! to a FOR variable. You can then use the FOR variable as the replacement string and use delayed expansion for the second step. This completely avoids the more brittle percent expansion.

for /f "delims=" %%A in ("!var2!") do echo !var1:SomeText=%%A!

以上方法之所以有效,是因为FOR变量扩展发生在延迟扩展之前.

The above works because FOR variable expansion takes place before delayed expansion.

到目前为止,这是我解决此问题的首选方法.

This is by far my preferred method to attack this problem.

有关批处理解析器各个阶段的更详尽说明,请参考jeb对 Windows命令解释器(CMD)的回答. .EXE)解析脚本?

For a more thorough explanation of the various phases of the batch parser, refer to jeb's answer to How does the Windows Command Interpreter (CMD.EXE) parse scripts?

这篇关于批处理脚本帮助-将DelayedExpansion Var的子字符串替换为另一个DelayedExpansion Var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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