- 1 year ago
Check for text variable substring in environment variable
To check for a text variable substring in an environment variable in Windows, you can use the following batch script:
bash@echo off
setlocal enabledelayedexpansion
set "var1=substring"
set "var2=%ENVIRONMENT_VARIABLE%"
if "!var2:%var1%=!"=="%var2%" (
echo Substring not found in environment variable
) else (
echo Substring found in environment variable
)
In this script, you need to replace substring
with the text variable substring you want to search for, and ENVIRONMENT_VARIABLE
with the name of the environment variable you want to search in.
The script uses the setlocal enabledelayedexpansion
command to enable delayed environment variable expansion, which allows you to use variables inside an if statement.
The if
statement checks whether the environment variable contains the text variable substring by using string substitution. The !var2:%var1%=!
expression replaces any occurrence of the var1
substring with an empty string, effectively removing it from the var2
variable. If the resulting string is the same as the original var2
variable, then the substring was not found in the environment variable. Otherwise, the substring was found in the environment variable.
The script outputs a message indicating whether the substring was found or not.