为什么找不到FindStr返回 [英] Why is FindStr returning not-found
问题描述
%vData%
等于包含;%main%\Programs\Go\Bin
的HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
变量.
%main%
是同一环境中的变量,其保留值C:\Main
.
%vData%
equals the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
variable which includes ;%main%\Programs\Go\Bin
.
%main%
is a variable in the same Environment which holds the value C:\Main
.
我想在添加之前检查此值是否存在,所以我有代码:
I want to check if this value exists before adding, so I have the code:
echo %vData% | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"
我尝试了%%main%%
和%main%
和%^main%
和%^^main%
,但是它始终告诉我该字符串不存在.
I have tried %%main%%
and %main%
and %^main%
and %^^main%
but it consistently tells me the string does not exist.
如何获取它以证明它确实存在?
How do I get it to show me it does exist?
推荐答案
鉴于该变量确实包含文字字符串;%main%\Programs\Go\Bin
(您可以通过简单地执行echo/%vData%
来证明它),我相信您正在寻找它:
Given that the variable really contains the literal string ;%main%\Programs\Go\Bin
(you can prove it by simply doing echo/%vData%
), I believe that you are looking for this:
echo/%%vData%%| findstr /I /C:"%%main%%\\Programs\\Go\\Bin"
,甚至更好,因为vData
中的特殊字符受到保护:
or, even better, since special characters in vData
become protected:
cmd /V /C echo(!vData!| findstr /I /C:"%%main%%\\Programs\\Go\\Bin"
您的代码有问题:
The problem with your code:
echo %vData% | FindStr /L /I /C:"%%main%%\\Programs\\Go\\Bin"
是在新的cmd
实例中执行管道|
的任一侧的事实 在新的vData
扩展了两次:
is the fact that either side of the pipe the left side of the pipe |
is executed in a new cmd
instance|
is executed in a new cmd
instance since it is an internal command [updated due to the finding handled in this question], so variable vData
is expanded twice:
-
当执行整个命令行时,左侧变为:
when the whole command line is executed, the left side becomes:
echo ...;%main%\Programs\Go\Bin
在执行管道的左侧时,它变为:
when the left side of the pipe is executed, it becomes:
echo ...;C:\Main\Programs\Go\Bin
这篇关于为什么找不到FindStr返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!