CMD:管道ECHO设置/变量扩展变量 [英] CMD: piping ECHO to SET/ expanding variables in variables
问题描述
%×:〜12,3%
返回3个字符开始于12日在x变量的性格。
我一直在试图完成使用变量,而不是为 12
和 3
。
比方说, Y = 12
和 Z = 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.
你可以做的是组VAR = %% X:〜%Y%,%Z %%%
。这将扩大内部变量是
和以Z
,而不是 X
,使 VAR
的值为%×:〜12,3%
。
手头剩下的工作现在是最后展开%×:〜12,3%
。我一直试图在一开始追加回声
让 VAR =回声%×:〜12,3%
。
如果在命令行或批处理文件,你现在用%VAR%
,这应该执行echo命令,并展开后续的前pression,但它不,而不是回声%X:〜只是中
被打印到在屏幕上,未展开。 12.3%
成果%×:〜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 %×:〜12,3%
,那么它呼应
和管道输出到另一个 ECHO
命令或 SET
命令的前pression将扩大,但它似乎 ECHO
和 SET
不接受数据被管道输送到它呢?
I was thinking that maybe if you set var to %x:~12,3%
, then echo it
and pipe the output into another ECHO
command or SET
command that the expression would be expanded, but it seems that ECHO
and SET
doesn't accept data being piped into it at all?
我怎样才能使这项工作?
How can I make this work?
推荐答案
我复制下面的整个文本从<一个href=\"http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990\">this回答;我只是改变的变量和特定实例名称以匹配这个问题的的:
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:
%×:〜12,3%
返回3个字符开始于12个字符x中
变量。我一直在试图完成使用变量是
而不是 12
和 3
。比方说, Y = 12
和 Z = 3
。
%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 of12
and3
. Let's sayy=12
andz=3
.
如果你想使用的另一个变量的的子串位置和lenght,那么你必须知道,更换其值包含在百分比变量被解析的从左至右;这意味着:%X:〜%Y%,%Z %%
没有得到期望的结果,因为它的意思是:显示的价值X:〜
变量,然后按是
,后跟的值,
变量,等等。
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.
要解决这个问题,你必须使用的延迟扩展的,就是插在年初 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%!
您也可以使用的参数为指标的命令:<!code> FOR / F令牌= 1,2%% i的(%Y%%Z%)并设置VAR = X :!〜我%%,%%Ĵ
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内的指数变化与呼叫
用双百分比和$ P $变量pcede命令。例如,在一个随机的Ÿ显示一个子0和12 lenght
位置:以Z
之间
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%%%
另一种方式来实现previous进程正在使用一个附加的用于命令改变由等效替换的参数的索引的延迟扩展,然后使用原始变量的延迟扩展。这种方法跑得比previous 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设置/变量扩展变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!