找出一个环境变量是否包含一个子 [英] Find out whether an environment variable contains a substring
问题描述
我需要找出是否有一定的环境变量(假设美孚)包含的子串(比方说BAR)在Windows批处理文件。有没有办法做到这一点只使用默认安装,Windows批处理文件命令和/或程序/命令?
I need to find out if a certain environment variable (let's say Foo) contains a substring (let's say BAR) in a windows batch file. Is there any way to do this using only batch file commands and/or programs/commands installed by default with windows?
例如:
set Foo=Some string;something BAR something;blah
if "BAR" in %Foo% goto FoundIt <- What should this line be?
echo Did not find BAR.
exit 1
:FoundIt
echo Found BAR!
exit 0
我应该标记线之上是让这个简单的批处理文件打印找到BAR?
What should the marked line above be to make this simple batch file print "Found BAR"?
推荐答案
当然,只要使用好老FINDSTR:
Of course, just use good old findstr:
echo.%Foo%|findstr /C:"BAR" >nul 2>&1 && echo Found || echo Not found.
而不是回声
ING你也可以在其他地方分支,但我想,如果你需要根据多个语句,下面是简单:
Instead of echo
ing you can also branch elsewhere there, but I think if you need multiple statements based on that the following is easier:
echo.%Foo%|findstr /C:"BAR" >nul 2>&1
if not errorlevel 1 (
echo Found
) else (
echo Not found.
)
编辑:记的<一个href=\"http://stackoverflow.com/questions/5491383/find-out-whether-an-environment-variable-contains-a-substring/5492033#5492033\">jeb's解决方案以及哪个更简洁,但它需要额外的精神一步找出它做什么阅读时。
Take note of jeb's solution as well which is more succinct, although it needs an additional mental step to figure out what it does when reading.
这篇关于找出一个环境变量是否包含一个子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!