I'm trying to set a variable to another variable's value with a substring removed. This happens inside a couple of for
loops, which I'll illustrate below.
setlocal EnableDelayedExpansion
for %%e in (a b c d) do (
set outerLoop=%%e
echo Handling !outerLoop!
dir *_!outerLoop! /S /B >inner-loop-files.txt
for /F "tokens=*" %%A in (inner-loop-files.txt) do (
set innerLoopFile=%%A
set search=_!outerLoop!
REM Not working!
REM set replacedFile=!innerLoopFile:search=!
echo !innerLoopFile!
echo !replacedFile!
)
)
I'm expecting output like this:
Handling a C:\Directory\File.ext_a C:\Directory\File.ext
I know the marked line in the innermost loop is close, but I can't quite get the right expansion syntax. When I do this, it works for the single example (though since it's not using a variable, it's wrong for the other iterations):
set replacedFile=!innerLoopFile:_a=!
What do I need to do to this expression for search
to expand properly?
set replacedFile=!innerLoopFile:search=!