2

Suppose there's a text file:

abc %x%
def %y%

and a batch file:

setlocal
set x=123
set y=456
for /f %%i in (textfile.txt) do echo %%i

The output will be

abc %x%
def %y%

Is there any (simple) way to get this (without powershell or special executables)?

abc 123
def 456
1
  • 1
    for /f "delims=" %%i in (textfile.txt) do call echo %%i Commented Dec 10, 2016 at 2:38

2 Answers 2

2

There is a simpler way with the call

@Echo off
set x=123
set y=456
for /f "delims=" %%i in (textfile.txt) do call call echo %%i

A double call is only necessray if there are double percent signs surrounding the var. But they do harm only if you use double percent signs to halt expansion one level.

Sign up to request clarification or add additional context in comments.

7 Comments

my actual solution used "call set" as my actual goal was never to echo the data. I never did try "for...do call call".
Why two CALLs? A single CALL will do
@dbenham no, did you test it? I did.
Yes, I have confirmed that it works as I expected. I took your exact code, removed one CALL, and it continued to work.
@LotPings Even for Win10 your described behaviour would be very unexpected, as a CALL restarts the parser at the perecent expansion phase.
|
1

You can use an extra level of calling to evaluate the text string as follows:

@echo off
setlocal
set x=123
set y=456
for /f "delims=" %%i in (textfile.txt) do call :evalecho %%i
endlocal
goto :eof

:evalecho
    echo %*
    goto :eof

The output of that for you input file is, as requested:

abc 123
def 456

2 Comments

Took me some time to remember that you can force a second evaluation with stacked pseudo calls.
I picked this answer because it was first and I never knew about the "call :label" syntax so it was also additionally educational.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.