42

I am writing a batch file where I need to output a string containing '!' to another file. But when I echo that string to another file, it removes "!" from the output.

Eg: Input:

set LINE=Hi this is! output
echo !LINE!>>new_file.txt

Output in new_file.txt is:

Hi this is output

Also, if input is

set LINE=Hello!! this is output!!
echo !LINE!>>new_file.txt

Output in new_file.txt:

Hello

Hence, it skips the ! (Exclamation mark) from the output to the new_file. If I use %LINE%, then it simply displays "echo is on" to the output file.

Please suggest a way to overcome this problem.

Vishal
  • 431
  • 1
  • 4
  • 5
  • 1
    When `echo %LINE%` outputs `echo is on` then you must be in a block. You should add a bit more of your code – jeb Jan 16 '13 at 09:43
  • 6
    The question is very misleading, you don't want to know the difference, you want to know how to escape it... – Dan Chaltiel Jul 08 '17 at 08:21

2 Answers2

25

If you have delayed expansion enabled and want to output an exclamation mark, you need to escape it.

Escaping of exclamation marks needs none, one or two carets, depending on the placement.

@echo off
REM No escaping required, if delayed expansion is disabled
set test1=Test1!

setlocal EnableDelayedExpansion
REM One caret required
REM Delayed expansion uses carets independent of quotes to escape the exclamation mark
set "test2=Test2^!"

REM Two carets required
REM The first caret escapes the second caret in phase2 of the parser
REM Later in the delayed expansion phase, the remaining caret escapes the exclamation mark
set test3=Test3^^!


echo !test1!
echo !test2!
echo !test3!

The difference between !var! and %var% in blocks is explained at DOS batch: Why are my set commands resulting in nothing getting stored?

An explanation of the batch parser can be found at How does the Windows Command Interpreter (CMD.EXE) parse scripts?

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    Imho, your link "Why are my set commands..." does not quite explain the difference between `!var!` `%var%` but rather the need for _EnableDelayedExpansion_. NOI, but I think Magoo's answer on [this question](https://stackoverflow.com/questions/42713625/delayed-expansion-and-exclamation-marks-in-strings/42714066#42714066) nails it better since it states explicitly that delayed expansion requires the exclamation mark for variable expansion. Just setting `EnableDelayedExclamation` is not sufficient. Probably plain and obvious for you Batch Gurus, but for us tiny worms... ;-) – Twonky Jan 16 '20 at 09:39
7

It seems you have called SETLOCAL EnableDelayedExpansion somewhere higher in the code. Take a look here to see what the effects from that are.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thank you for the answer.Yes, I am using this property in my file as this code is in a for loop and also I am performing some find/replace operatios on this string so I have to use it. – Vishal Jan 16 '13 at 09:25
  • @User the link I sent gave you gives good explanation on the side effects of that. I hope it will help you understand the behavior you are observing. – Ivaylo Strandjev Jan 16 '13 at 09:27