3

I want to put the following content into the file: <ScriptFile Make="3">
It fails for the reason of the string containing the angle brackets < and > and the double quote character ".

I have tried escaping the characters following way: ^<ScriptFile Make=""3""^>
It worked, but the output in the file was exactly the same as the escaped string.

The code snippet:

@echo off

set TEMP="^<ScriptFile Make=""3""^>"
echo %TEMP% > gen.xml
pause

How can I output the string value of TEMP variable into file gen.xml without loosing the double quotes and the angle brackets?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Lucas
  • 3,517
  • 13
  • 46
  • 75

3 Answers3

3

You can extract the angle brackets out of the variable, like this:

@echo off
set TEMP1=ScriptFile Make="3"
echo ^<%TEMP1%^> > gen.xml
pause

This way, the brackets can be escaped properly, you do not need any special escaping for the string put in the variable and the gen.xml looks like expected:

D:\temp>type gen.xml
<ScriptFile Make="3">
keenthinker
  • 7,645
  • 2
  • 35
  • 45
1

This worked for me:

@echo off

set "TEMP=^<ScriptFile Make="3"^>"
echo %TEMP% > gen.xml
pause

Another method would be to use delayed expansion:

@echo off

set "TEMP=<ScriptFile Make="3">"
setlocal EnableDelayedExpansion
echo !TEMP! > gen.xml
endlocal
pause
Andriy M
  • 76,112
  • 17
  • 94
  • 154
0

TEMP should not be used as environment variable name because TEMP is an important environment variable predefined by Windows. It has as value the name of the directory for temporary files of current user account with complete path. For details see Wikipedia article Windows Environment Variables.

One method is using delayed expansion as suggested also by Andriy M.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "TempVar=<ScriptFile Make="3">"
echo !TempVar!>gen.xml
endlocal

First a local environment is created for the next two command lines with command extensions and delayed expansion of environment variables enabled which are both needed here. Command extensions are enabled by default, but delayed expansion is disabled by default. See this answer explaining in detail what the commands SETLOCAL and ENDLOCAL do.

The string <ScriptFile Make="3"> is assigned next to environment variable TempVar. There is no need to escape the angle brackets or the double quotes. For a detailed explanation why there is no need to escape anything in this string and why first double quote character is left of variable name and not after equal sign read this answer.

The value of the environment variable is output by command ECHO with redirecting this output with the redirection operator > into the file gen.xml using delayed expansion.

There is no space character between second exclamation mark ! and redirection operator >. This avoids writing also a trailing space after <ScriptFile Make="3"> into the file. 1 or more space characters between > and file name gen.xml are ignored on parsing this command line. But any whitespace character left of redirection operator > is also output by command ECHO and for that reason also written into the file.

Another method is not using an environment variable at all and escape the angle brackets with character caret ^ as demonstrated below:

@echo off
echo ^<ScriptFile Make="3"^>>gen.xml
endlocal

Double quotes must not be escaped on using command ECHO as in this special case the double quote characters are interpreted as literal characters.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • set /?
  • setlocal /?

And read also the Microsoft TechNet article Using command redirection operators.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143