By registering with us, you'll be able to discuss, share and exchange private messages with other members of our community.

SignUp Now!

How does this work?

I can't figure out how this works but it manages to get an escape character into an environment variable in CMD.

The delims parameter delimits the text. So it parses the #$H#$E# into %%a==$H %%b=$E. But there is also no delayed expansion, so my guess is that the for %%b in (1) is essentially ignored. And the set ESC statement is working on the parsed token.
It looks to me like the first FOR is processing the output of the command

"prompt #$H#$E# & echo on & for %%b in (1) do rem"

Maybe I get it now. Because that command turns ECHO ON, the output of that command is (would be) the (new) prompt followed by the "rem". That would be #<backspace>#<esc>#rem and the outer FOR sets the variable ESC to the second token.

The surprising part is that the prompt itself is part of the (would-be) output of "prompt #$H#$E# & echo on & for %%b in (1) do rem".

[And maybe not.]
It doesn't work if you run it from a command prompt as opposed to from a batch file.