The backslash has nothing to do with your problem. There is also no problem with assigning a variable. :-)
Your problem - the &
character - is a compound command statement that allows you to run multiple commands on the same input line. You want &
be a literal, but it is treated as an operator when you try to execute ECHO. For example, echo this&echo that
displays two lines of output: "this", and then "this."
The batch line must be analyzed before it can be completed. The parser must identify the commands and operators. The %texty%
occurs before the commands and operators are parsed, so the parser reads the next token after the &
command.
There are many special characters that can cause similar problems: |
>
<
)
&
^
.
There are two ways to make the parser-parser treat a special character as a literal: 1) enclose it in quotation marks or 2) avoid it with ^
.
echo this&echo that echo "this & that" echo this ^& that
results:
this that "this & that" this & that
In your SET statement, you have all the purpose enclosed in quotation marks, so it is correctly interpreted as a literal. You can prove it by yourself by placing the set texty
after your specified assignment - it will display the current texty value on the screen and you will see that it is your desired value.
But when you repeat the value, it is no longer quoted, so the parser treats &
as an operator, and you have a glitch.
You can echo "%texty"
, but then you will get quotes in your release.
You can change the definition of texty to include an escape character.
set "texty=\Music\TheArchive\Aeph ^& Neonlight\Aeph ^& Neonlight - Space Truckers - 7b - 173.26.mp3"
But it is a pain. Fortunately, there is a simple solution - slow expansion of a variable occurs after the commands and operators have been parsed, so you no longer need to worry about special characters.
A delayed extension must be enabled before using it with setlocal enableDelayedExpansion
. Then you include the variable name in exclamation points instead of percentages.
setlocal enableDelayedExpansion set "texty=\Music\TheArchive\Aeph & Neonlight\Aeph & Neonlight - Space Truckers - 7b - 173.26.mp3" echo !texty! >>C:\Music\Playlists\baseplaylist.m3u
Now the string is parsed for commands and operators before the extension !texty!
. Thus, ECHO correctly prints the literal value texty.