2
for /f skip^=4 %b in ('"echo;prompt;$E|cmd"') do set ESC=%b

If I redirect this command to a file I can see that there are 4 CR/LF in the file.

The last characters in the file are in HEX 0D 0A 0D 0A 1B

Why is the escape character ^ before the equal sign needed??? viz. skip^=4

Io-oI
  • 9,237

2 Answers2

0

See How-to: Escape Characters, Delimiters and Quotes at the Windows command line.

The ^ character is the escape character. Adding it before a special character allows this character to be treated as ordinary text.

But you may get rid of the escape character by enclosing the text in quotes, such as:

for /f "skip=4" %%b ...
harrymc
  • 498,455
0

for /F %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"


  • Below is my adaptation avoiding four unnecessary lines with "skip=4", which can also be written without double quotes but needs to be properly escaped on the equals sign, where we use ^:

for /f skip^=4 %b in ('"echo;prompt;$E|cmd"') do set "_ESC=%b"


Output to stdout

>for /f %a in ('"echo;prompt;$E|cmd"') do @echo\output: %a
output: Microsoft
output: (c)
output: C:\Windows\System32>prompt;$E
output:

>


>for /f skip^=4 %a in ('"echo;prompt;$E|cmd"') do @echo\output: %a
output:

>


Output to file:

for /f skip^=4 %a in ('"echo;prompt;$E|cmd"') do @echo;%a>>File_With_Skip4.txt
  • Output file content .\File_With_Skip4.txt:
    enter image description here

  • File content in Hex .\Hex_File_With_Skip4.txt:
    enter image description here

for /f %a in ('"echo;prompt;$E|cmd"') do @echo;%a>>File_Without_Skip4.txt
  • Output file content .\File_Without_Skip4.txt:
    enter image description here

  • File content in Hex .\Hex_File_Without_Skip4.txt:
    enter image description here


Obs.1: Below command line converts the file to .\Hex_File_Without_Skip4.txt:

CertUtil -EncodeHex File_Without_Skip4.txt Hex_File_Without_Skip4.txt

Obs.2: The images that display "non-printable" characters here were obtained using Notepad++ and enabling the display of all characters:

enter image description here


Obs.3: See Commands Redirection for the behavior of > amd >> what is relevant to the point:

I can see that there are 4 CR/LF in the file.
If you are seeing 4 CR/LF even using skip^=4, please check your command and make sure you are overwriting or adding content to the output file.


Why is "skip=4" or skip=^4 needed - and not just skip=4?


When I redirected I only redirected the in( ) part of the, for /f command: Viz. C:\>echo;prompt;$E|cmd > result.txt

The CMD command calls the shell and by default returns the first 4 lines, after which it handles the redirected argument (echo;prompt;|), which produces/returns the Esc in the last (fifth) line:

enter image description here

Understand that the commands are treated by the command interpreter, and this uses predefined syntax rules, and in many occasions the use of one or more characters can be understood as a command complement, as an operator, as a redirector , etc. .. These characters assume a different behavior than the literal use, the documentation calls them special characters, and they can and will interfere differently if we use them without the escapes.

For more information, please read the linked references above.


Additional resources:

Io-oI
  • 9,237