I need to find out if a certain 开发者_Go百科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?
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"?
Of course, just use good old findstr:
echo.%Foo%|findstr /C:"BAR" >nul 2>&1 && echo Found || echo Not found.
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.
)
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.
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
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 we don't need to add any % character to our variables after the "FindString" function. It's automatically done internally in our custom function.
(It's a great solution for Batch functions that allows us to easily use values that can contain spaces for the function arguments without needing to care about quotes or anything like that.)
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
精彩评论