Consider the following code executed on a Windows (10) cmd (this is actually a simplyfied version of a script that shows an erronous behavior):
type nul >> "a"
set "e=" & for /f "delims=" %F in ('dir /b a') do (set "e=%F" & if not defined e (echo "") else (echo "%e%"))
In short: reset the variable e
, loop over files found by dir
, make e
the current filename, if e
is defined, print it.
Run for the first time, it produces the output %e%
. When executed a second time in the same shell, it produces a
(which is what I would have expected). This doesn't happen when there is no for
involved.
What causes this behavior and how do I always get the correct output?