windows 找出环境变量是否包含子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5491383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:14:43  来源:igfitidea点击:

Find out whether an environment variable contains a substring

windowsbatch-file

提问by LCC

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?

我需要找出某个环境变量(比如 Foo)是否在 Windows 批处理文件中包含一个子字符串(比如 BAR)。有什么方法可以仅使用 Windows 默认安装的批处理文件命令和/或程序/命令来做到这一点?

For example:

例如:

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

What should the marked line above be to make this simple batch file print "Found BAR"?

上面的标记行应该是什么才能使这个简单的批处理文件打印“Found BAR”?

回答by Joey

Of course, just use good old findstr:

当然,只需使用良好的旧 findstr:

echo.%Foo%|findstr /C:"BAR" >nul 2>&1 && echo Found || echo Not found.

Instead of echoing you can also branch elsewhere there, but I think if you need multiple statements based on that the following is easier:

除了echoing 您还可以在那里的其他地方分支,但我认为如果您需要基于以下内容的多个语句,则更容易:

echo.%Foo%|findstr /C:"BAR" >nul 2>&1
if not errorlevel 1 (
   echo Found
) else (
    echo Not found.
)

Edit:Take note of jeb's solutionas well which is more succinct, although it needs an additional mental step to figure out what it does when reading.

编辑:还要注意jeb 的解决方案,它更简洁,尽管它需要额外的心理步骤来弄清楚它在阅读时的作用。

回答by jeb

The findstrsolution works, it's a little bit slow and in my opinion with findstryou break a butterfly on a wheel.

findstr解决方案的工作,这是一个有点慢,在我和意见findstr你打破在车轮上的蝴蝶。

A simple string replace should also work

一个简单的字符串替换也应该工作

if "%foo%"=="%foo:bar=%" (
    echo Not Found
) ELSE (
    echo found
)

Or with inverse logic

或者用逆逻辑

if NOT "%foo%"=="%foo:bar=%" echo FOUND

If both sides of the comparision are not equal, then there must be the text inside the variable, so the search text is removed.

如果比较的两边不相等,那么变量里面肯定有文本,所以去掉搜索文本。

A small sample how the line will be expanded

该行将如何扩展的小示例

set foo=John goes to the bar.
if NOT "John goes to the bar."=="John goes to the ." echo FOUND

回答by niklausu

@mythofechelon: The %var:str=% part removes strfrom var. So if varcontains stron the left side of the equation, it will be removed on the right side - thus the equation will result in "false" if strwas found in varor "true" if strwas not present in var.

@mythofechelon: %var:str=% 部分从var 中删除str。所以,如果VAR包含STR在等式的左边,将右侧去除-因此方程会导致“假”如果海峡是在发现VAR或“真”如果海峡是不存在的变种

回答by Frank Einstein

I wrote this function for a nice script integration. Code looks better and it's also easier to remember. This function is based on Joey's answer on this page. I know it's not the fastest code but it's seems to work very well for what I need to do.

我为一个很好的脚本集成编写了这个函数。代码看起来更好,也更容易记住。此功能基于 Joey 在此页面上的回答。我知道这不是最快的代码,但它似乎非常适合我需要做的事情。

Just copy the function's code at the end of your script and you can use it like in this example here:

只需复制脚本末尾的函数代码,您就可以像在此示例中一样使用它:

Example:

例子:

set "Main_String=This is just a test"
set "Search_String= just "

call :FindString Main_String Search_String

if "%_FindString%" == "true" (
    echo String Found
) else (
    echo String Not Found
)

Note that you don't need to add % to your variables when giving them to this function, it will automatically take care of this. (This is a method that I found which it letting me use spaces in my function's arguments/variables without the need of using undesirables quotes in them.)

请注意,将变量提供给此函数时,您无需将 % 添加到变量中,它会自动处理此问题。(这是我发现的一种方法,它让我可以在函数的参数/变量中使用空格,而无需在其中使用不需要的引号。)

Function:

功能:

:FindString

rem Example:
rem 
rem set "Main_String=This is just a test"
rem set "Search_String= just "
rem 
rem call :FindString Main_String Search_String
rem 
rem if "%_FindString%" == "true" echo Found
rem if "%_FindString%" == "false" echo Not Found

SETLOCAL

for /f "delims=" %%A in ('echo %%%1%%') do set str1=%%A
for /f "delims=" %%A in ('echo %%%2%%') do set str2=%%A

echo.%str1%|findstr /C:"%str2%" >nul 2>&1
if not errorlevel 1 (
   set "_Result=true"
) else (
   set "_Result=false"
)

ENDLOCAL & SET _FindString=%_Result%
Goto :eof