我有点挣扎于cmd.exe,所以让我把它放在代码中:
:: Set an environment variable var to `pwd`
:: (forget about the backslashes)
C:\somedir>set var=C:\\\somedir\\\ & call echo %var%
C:\\\somedir\\\
:: great! works.
:: Is the environment variable really there?
C:\somedir>set v
var=C:\\\somedir\\\
:: yes. ok.
:: Traverse directory recursively, find class files ending with T,
:: delete `pwd` from filenames:
C:\somedir>dir /s /b *T.class | grep T\. | sed 's/%var%//g'
C:\somedir\domain\test\ClassT.class
:: uh oh no! Didn't work.
:: Wait a minute. Is the environment variable really there?
C:\somedir>set v
var=C:\\\somedir\\\
:: hmm...yes, indeed?
:: ok. Take a breath. Start over. Empty/Delete var.
C:\somedir>set var=
:: Manually fill var
C:\somedir>set var=C:\\\somedir\\\
:: Try again...
C:\somedir>dir /s /b *T.class | grep T\. | sed 's/%var%//g'
domain\test\ClassT.class
::YES! uh...oh...no...w**hy does it not work above?
问题是,为什么我不能在管道中使用var
虽然它在环境中可用,但是当我设置var“手动”而不是set var=C:\\\somedir\\\ & call echo %var%
时,会发生什么变化,对于第一部分完全合法的手册,如下面的清单。
答案 0 :(得分:2)
在这种情况下,您实际上将var设置为c:\\\somedir\\\<whitespace>
是的,cmd在设置变量方面并不是很聪明。
在后一个示例中,您可能会在到达“\”后立即按Enter键。 确定 - 使用():
(SET VAR=c:\\\windows\\\) & echo %VAR%
:: Test for trailing space...
echo [%VAR%]