将该变量设置为for语句中的另一个变量 [英] Set the variable to another variable in the for statement

查看:281
本文介绍了将该变量设置为for语句中的另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将for语句的变量设置为另一个变量。当我这样做时:

$ p $ for / ftokens = * delims =%% p in(plugins.txt)设置pp = %% p

它的工作原理是正确的。但是我想做更多的事情,所以我做了之后(现在它不工作,为什么是这样的,我该如何解决它?)

  for / ftokens = * delims =%% p in(plugins.txt)do(
set pp = %% p
echo%pp%
echo %pp%
echo %% p)

编辑:输出

内容plugins.txt

http:// subversion / svn / dotCMSPlugins / CustomLogin / trunk



所需输出

CustomLogin

解决方案

这个设置可以工作,但是扩展的工作方式和预期的一样。

扩展的百分比将在解析括号块的时刻完成,而不是在执行单个命令的那一刻。 cmd.exe如何解析脚本

在set语句中使用空格不是一个好主意,因为它cr在你的情况下给一个变量赋一个空格 set pp = %% p 创建一个名为 pp的变量< space> 值为< space>< %% p 的内容。

您可以使用延迟扩展来获得所需的结果。

  setlocal EnableDelayedExpansion 
for / ftokens = * delims =%% p in(plugins.txt) (
setmyVar = %% p
setmyVar =!myVar:subversion / svn / dotCMSPlugins / =!
setmyVar =!myVar:/ trunk =!
echo!myVar!


I'm trying to set the variable of of the for statement to another variable. When I'm doing this:

for /f "tokens=* delims= " %%p in (plugins.txt) do set pp = %%p

It works correct. But I want to do more, So I do it after the ( and now it doesn't work. Why is that and how can I solve it?

for /f "tokens=* delims= " %%p in (plugins.txt) do (
set pp = %%p
echo %pp%
echo %pp%
echo %%p)

EDIT: Added input and desired output

content of plugins.txt
http://subversion/svn/dotCMSPlugins/CustomLogin/trunk

Desired output
CustomLogin

解决方案

The setting works, but the expansion works in an other way as expected.

The percent expansion will be done in the moment of parsing the parenthesis block, not in the moment of execution of the single commands. How does cmd.exe parse scripts

Btw. Using spaces in the set statement isn't a good idea, as it creates a variable with a space, in your case set pp = %%p creates a variable named pp<space> with a value of <space><content of %%p.

You can get the desired results by using delayed expansion.

setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%p in (plugins.txt) do (
  set "myVar=%%p"
  set "myVar=!myVar:subversion/svn/dotCMSPlugins/=!"
  set "myVar=!myVar:/trunk=!"
  echo !myVar!
)

这篇关于将该变量设置为for语句中的另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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