Neil Weicher
Neil Weicher

Reputation: 2502

Why doesn't this batch script work? Reading lines from file

ok, I give up. Why doesn't this work?

setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /F %%I in (myfile.txt) do (
    echo I: %%i
    set LINE=%%i
    echo LINE: %LINE%
)

"echo I:" displays the lines correctly, but "echo LINE:" is empty

I have tried different variations with the same results, such as

    set LINE=%i
    set LINE=%i%
    set LINE=!i!

Obviously there is something simple I am not understanding.

Upvotes: 0

Views: 85

Answers (1)

Stephan
Stephan

Reputation: 56180

you enabled delayed expansion, so the only thing you have to do is: use it.

replace echo LINE: %LINE% with echo LINE: !LINE!

EDIT: solution without delayed extension

FOR /F %%I in (myfile.txt) do ( call DoIt %%I )

exit /b
:DoIt
    echo I: %1
    set LINE=%1
    echo LINE: %LINE%
goto :eof

Upvotes: 3

Related Questions