本文介绍了CMD:管道 ECHO 到 SET/扩展变量中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%x:~12,3% 返回从 x 变量中第 12 个字符开始的 3 个字符.我一直试图完成的是使用变量而不是 123.假设 y=12z=3.那么,就不能使用%x:~%y%,%z%%,因为CMD会认为%x:~%是一个变量.

%x:~12,3% Returns 3 characters starting at the 12:th character in x variable.What I have been trying to accomplish is using variables instead of 12 and 3.Let's say y=12 and z=3.Then, you can't use %x:~%y%,%z%%, because CMD will think %x:~% is a variable.

你可以做的是set var=%%x:~%y%,%z%%%.这将扩展内部变量yz,但不会扩展x,因此var 的值为%x:~12,3%.现在剩下的任务是最终扩展%x:~12,3%.我一直试图在开头附加 echo 以便 var=echo %x:~12,3%.如果在命令行或批处理文件中您现在使用 %var%,这应该执行 echo 命令,并扩展后续表达式,但它没有,而是 echo %x:~12,3% 导致简单的 %x:~12,3% 被打印到屏幕上,未展开.

What you can do is set var=%%x:~%y%,%z%%%. This will expand the inside variables y and z, but not x, so that the value of var is %x:~12,3%.The remaining task at hand now is to finally expand %x:~12,3%. I have been trying to append echo in the beginning so that var=echo %x:~12,3%.If at the commandline or in a batch file you now use %var%, this should execute the echo command, and expand the succeeding expression, but it doesnt, instead echo %x:~12,3% results in simply %x:~12,3% being printed to the screen, unexpanded.

我在想,也许如果您将 var 设置为 %x:~12,3%,然后回显它并将输出通过管道传输到另一个 ECHO 命令或 SET 命令中,表达式将被扩展,但似乎 ECHOSET 根本不接受通过管道传输的数据?

I was thinking that maybe if you set var to %x:~12,3%, then echo itand pipe the output into another ECHO command or SET command that the expression would be expanded, but it seems that ECHOand SETdoesn't accept data being piped into it at all?

我怎样才能做到这一点?

How can I make this work?

推荐答案

我从 这个答案;我只是更改了变量和特定示例的名称以匹配这个问题的名称:

I copied the entire text below from this answer; I just changed the names of variables and particular examples to match the ones of this question:

%x:~12,3% 返回从 x 中的第 12:th 个字符开始的 3 个字符多变的.我一直试图完成的是使用变量而不是 123.假设 y=12z=3.

如果你想使用另一个变量作为子串的位置和长度,那么你必须知道用它们的值替换百分号包围的变量是从左到右解析>;这意味着: %x:~%y%,%z%% 没有给出想要的结果,因为它意味着:显示 x:~ 变量的值,后跟y,后跟变量的值等


If you want to use another variables for substring position and lenght, then you must know that the replacement of variables enclosed in percents by their values is parsed from left to right; this mean that: %x:~%y%,%z%% don't give the desired result because it mean: show the value of x:~ variable, followed by y, followed by the value of , variable, etc.

要解决这个问题必须使用Delayed Expansion,即在开头插入setlocal EnableDelayedExpansion命令,子串变量用百分号括起来,原变量用感叹号括起来标记:

To solve this problem you must use Delayed Expansion, that is, insert setlocal EnableDelayedExpansion command at beginning, enclose substring variables in percents, and enclose the original variable in exclamation marks:

setlocal EnableDelayedExpansion
set x=0123456789ABCDEF
set y=12
set z=3
set var=!x:~%y%,%z%!

你也可以使用 FOR 命令的参数作为索引:for/F "tokens=1,2" %%i in ("%y% %z%") do set var=!x:~%%i,%%j!.

You may also use parameters of FOR commands as indexes: for /F "tokens=1,2" %%i in ("%y% %z%") do set var=!x:~%%i,%%j!.

当 FOR/IF 中的索引更改时获取子字符串的值,将变量括在双百分比中,并在命令之前使用 call.例如,要在 0 到 12 和长度 z 之间的随机 y 位置显示子字符串:

To get the value of a substring when the index change inside FOR/IF enclose the variable in double percents and precede the command with call. For example, to show a substring at a random y position between 0 and 12 and lenght z:

if %some% == %test% (
   set /A y=!random! %% 13
   call echo %%x:~!y!,%z%%%
)

您也可以在括号外使用此方法以避免延迟扩展:

You may also use this method outside parentheses in order to avoid the Delayed Expansion:

call echo %%x:~%y%,%z%%%

另一种实现前面过程的方法是使用一个额外的 FOR 命令,通过一个等效的可替换参数来改变索引的延迟扩展,然后对原始变量使用延迟扩展.此方法比之前的 CALL 运行得更快:

Another way to achieve previous process is using an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the original variable. This method run faster than previous CALL:

if %some% == %test% (
   set /A y=!random! %% 13
   for %%y in (!y!) do echo !x:~%%y,%z%!
)

这篇关于CMD:管道 ECHO 到 SET/扩展变量中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:59
查看更多