Batch string concatenation

I am trying to create a batch line like this: abcd _

I have a variable called soeid, with a value like abcd. So this is what I do, but it does not work.

set soeid=abcd set "val1=>" set "val2=_" set "str=%val1%%soeid%%val2%" echo %str% 
+7
source share
1 answer

I am sure it is working fine. To prove this, add SET STR after defining the value, and you will see the correct value.

The problem you are facing is trying to repeat the echo of the value, the line being executed becomes: echo >abcd_ . > not quoted or escaped, so it just takes the ECHO output without arguments and redirects it to a file called "abcd _"

If you don't mind looking at quotes, then change your line to echo "%str%" and it will work.

Another option is to enable and use the delayed extension (I assume this is a batch script code and does not execute on the command line)

 setlocal enableDelayedExpansion set soeid=abcd set "val1=>" set "val2=_" set "str=%val1%%soeid%%val2%" echo !str! 

Normal %var% expansion occurs early, while the interpreter parses the string. Deferred extension !var! occurs at the end before its execution. The redirection is found somewhere in the middle. That is why the normal extension does not work - the interpreter sees the extended > character and interprets it as an output redirection operator. A delayed extension hides the > character from the interpreter until the redirection is analyzed.

For more information about the delayed extension, enter SET /? from the command line and read, starting with the paragraph that begins with "Finally, support for slow expansion of environment variables ...".

+11
source

All Articles