This is adapted with permission from the author of the Batch Files - Escape Symbols page on the scripting site of Rob van der Voude .
TLDR
Escaping batch file characters on Windows (and DOS) is difficult :
As in the universe, if someone ever fully understands the Package, then the language will be immediately replaced by an infinitely stranger and more complex version of itself. This obviously happened at least once before;)
Percent sign %
% can be escaped as %% - "It may not always be required [escaped] in double-quoted strings, just try"
Usually use a carriage ^
These characters "may not always be required to be [escaped] in double-quoted strings, but this will not hurt":
Example: echo a ^> b to print a > b on the screen
' " is required [must be escaped] only in the FOR /F "subject" (that is, between parentheses) , if backq is not used
' "is required [must be escaped] only in the FOR /F " subject "(that is, between parentheses) , if backq used"
These characters are "required [for escaping] only in the FOR /F " subject "(that is, between parentheses), even in double-quoted strings":
Double Escape exclamation points when using a deferred variable extension
! must be escaped ^^! when pending variable expansion is activated.
Double double quotes in find search patterns
" β ""
Use backslash in findstr regex patterns
Also
Rob commented on this question further (via email with me):
As for the answer, I'm afraid that the chaos is even worse than the original poster understands: the requirements for escaping brackets also depend on whether the line is inside the code block or not!
I believe that an automated tool can simply insert an insertion character in front of each character, and then double all the percent signs - and it still fails if the string is enclosed in double quotes!
In addition, individual programs are responsible for parsing their command line arguments, so some of the escaping is required, for example, for: for sed or ssed may be associated with specific programs invoked in batch scripts.