Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.bat file to create a complex string variable with special characters

Tags:

batch-file

I need to create a single string variable by concatenating multiple strings. The final string i need is as below

<Workspace name="RealTimeRiskUSD_UA" path="C:\workspace" IsAdmin="false" />

This is what i tried.

echo off
set path1="<Workspace "
set name="name="RealTimeRiskUSD_UA"" 
set path2="path="C:\workspace" IsAdmin="false" />"
set fullpath=%path1%%name%%path2%
echo %path1%
echo %name%
echo %path2%
echo %fullpath%

I also tried using the below link to remove the double quotes from each string but does not work
http://ss64.com/nt/syntax-esc.html

like image 911
NewQueries Avatar asked Feb 16 '12 20:02

NewQueries


People also ask

What is %% in a BAT file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

How do I set an environment variable in a batch file?

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set. If you call a variable value from a batch file, enclose the value with percent signs (%).

Can you use variables in batch files?

There are two types of variables in batch files. One is for parameters which can be passed when the batch file is called and the other is done via the set command.


1 Answers

You could use the extended syntax of SET. set "var=content".

This escapes special characters, but the quotes aren't part of the string.

echo off
Setlocal EnableDelayedExpansion
set "path1=<Workspace " 
set "name=name="RealTimeRiskUSD_UA"" 
set "path2=path="C:\workspace" IsAdmin="false" />" 
set "fullpath=%path1%%name%%path2%"
echo !fullpath!
like image 199
jeb Avatar answered Nov 14 '22 02:11

jeb