John's Computer Journal: Nested Environment Variables

Friday, April 20, 2007

Nested Environment Variables

The following workaround example shows how to nest environment variables inside of each other in a batch file. The nested variables can be used for specifying sub-string indices or replacements dynamically during runtime.

For example to remove the current directory from the path environment variable:

set path=%PATH:%CD%=%

does not work. However, the following code:

for /f "delims=" %%i in ('echo %%PATH:%CD%^=%%') do set path=%%i

will work fine.

References

No comments: