JSI Tip 9741. Using delayed expansion to expand nested environment variables?

Jerold Schulman

September 14, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today


In tip 4860, we used the CALL command to expand nested environment variables.

You can also use delayed expansion:

set work=!string:%substring%=!
if not "%work%" EQU "%string%" @echo %string% contains %substring%.

Using this approach, I have re-written the CheckPath.bat sample from tip 4860:

@echo offsetlocal ENABLEDELAYEDEXPANSIONif {%1}=={} @echo Syntax: CheckPath Folder&endlocal&goto :EOFset fold=%1set folder=%fold:"=%;set folder=%folder:\;=;%set found=N:: Retrieve System Path.for /f "Skip=2 Tokens=1,2*" %%i in ('REG QUERY "HKLMSystemCurrentControlSetControlSession ManagerEnvironment" /v PATH') do ( set syspath=%%k;)set work=!syspath:%folder%=!if "%work%" NEQ "%syspath%" set found=Y&goto ansset folder=%folder:;=;%set work=!syspath:%folder%=!if "%work%" NEQ "%syspath%" set found=Y:ans@echo %found%endlocal



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like