2

I'm trying to create and write inside a text file using batch file. The line that I'm trying to write is as below but I'm having a problem that batch is interpreting I'm trying to write inside a file here whereas it is actually the line that I want to write inside the file.

Can someone please help on how to make sure batch command just ignore it and just write inside the text file as it is.

The line is:

echo while () >>test.txt

So, I want to print these "<" and ">" inside the bracket as well in the txt file and don't want batch file interpret it as a command.

You guys help is really appreciate here!

Thanks Hijan

mhalwi
  • 31
  • 5
  • 3
    The escape character of Windows command interpreter is `^`. So put `^` left to an operator to get it interpreted as literal character. An exception is the percent sign `%`. This character must be escaped with duplicating the percent sign to be interpreted as literal character instead of begin/end of an environment variable, loop variable, or batch file argument reference. With delayed expansion enabled exclamation marks must be also escaped with two `^` to be interpreted as literal characters. Delayed expansion is by default not enabled. – Mofi Aug 29 '17 at 17:23
  • 1
    It works man.. I have try to tweak the script few ways already. really appreciated it!! – mhalwi Aug 29 '17 at 17:57

1 Answers1

1

As Mofi Mentioned, < is a special character and needs to be escaped by a ^.


I recommend reading this and this question as they are about the same topic. The following is an example taken from my question, answered by Mofi.

FOR Loop Implicated Delayed Expansion

The for loop metavariable %%n is quite different from other variables. FOR loop metavariable can change every time the loop runs.

for %%G in ("|%%!<>()") do echo %%~G>file.ext

This will echo |%!M<() into file.ext, notice the percentage sign stills needs to be doubled.

Community
  • 1
  • 1