Find out whether an environment variable contains a substring

后端 未结 4 1872
盖世英雄少女心
盖世英雄少女心 2020-12-16 14:21

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

相关标签:
4条回答
  • 2020-12-16 14:34

    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.

    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
    
    0 讨论(0)
  • 2020-12-16 14:40

    Of course, just use good old 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:

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

    Edit: 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.

    0 讨论(0)
  • 2020-12-16 14:42

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

    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
    
    0 讨论(0)
  • 2020-12-16 14:53

    @mythofechelon: The %var:str=% part removes str from var. So if var contains str on the left side of the equation, it will be removed on the right side - thus the equation will result in "false" if str was found in var or "true" if str was not present in var.

    0 讨论(0)
提交回复
热议问题