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

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 off
if \{%1\}==\{\} @echo Syntax: CheckPath Folder&endlocal&goto :EOF
set fold=%1
set folder=%fold:"=%\;
set folder=%folder:\\;=\;%
set found=N
:: Retrieve System Path.
for /f "Skip=2 Tokens=1,2*" %%i in ('REG QUERY "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PATH') do (
 set syspath=%%k;
set work=!syspath:%folder%=!
if "%work%" NEQ "%syspath%" set found=Y&goto ans
set folder=%folder:\;=;%
set work=!syspath:%folder%=!
if "%work%" NEQ "%syspath%" set found=Y
@echo %found%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.