Simply adding a CALL may solve your problem. (also your definition of VersionNumber was wrong)
SET VersionNumber=7.0
FOR /F "eol=; delims=" %%I IN (TargetFolders.txt) DO (
call echo Folder: %%I
)
But this will fail if your file contains unquoted special characters like &
, >
, <
, |
.
For example, the following line would fail:
%ProgramFiles%\This&That\ %VersionNumber%
It will work if it is quoted
"%ProgramFiles%\This&That\" %VersionNumber%
The CALL will also mangle any quoted carets: "^"
will become "^^"
The best solution would be to modify your text file and replace each %
with !
.
!ProgramFiles!\Acme\FooBar !VersionNumber!
!ProgramFiles!\This&That !VersionNumber!
Now you can safely use delayed expansion to expand the variables within the loop.
setlocal enableDelayedExpansion
SET VersionNumber=7.0
FOR /F "eol=; delims=" %%I IN (TargetFolders.txt) DO (
echo Folder: %%I
)
If your text file already has !
that you want to preserve, then it must be escaped. Also ^
will have to be escaped if it appears on a line with a !
.
preserve caret ^^ and exclamation ^! by escaping
caret ^ without exclamation is no problem
Alternatively you can substitute variables for caret and exclamation literals
alternate method to preserve caret !c! and exclamation !x!
caret ^ without exclamation still no problem
And then define the variables in your batch
setlocal enableDelayedExpansion
set "x=^!"
set "c=^"
SET VersionNumber=7.0
FOR /F "eol=; delims=" %%I IN (TargetFolders.txt) DO (
echo Folder: %%I
)