1

我对 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%第一部分完全合法,完全手动,如清单后面.

4

1 回答 1

2

在这种情况下,您实际上将 var 设置为c:\\\somedir\\\<whitespace> Yes,cmd 设置变量并不是很聪明。

在后一个示例中,您可能一到达“\”就按回车键。可以肯定的是 - 使用():

(SET VAR=c:\\\windows\\\) & echo %VAR%
:: Test for trailing space...
echo [%VAR%]
于 2013-02-12T20:53:16.800 回答