Writing a text string containing a backslash, variable assignment problems

I want the file \Music\TheArchive\Aeph & Neonlight\Aeph & Neonlight - Space Truckers - 7b - 173.26.mp3 take this text \Music\TheArchive\Aeph & Neonlight\Aeph & Neonlight - Space Truckers - 7b - 173.26.mp3 and wrote it to a new line in the existing baseplaylist.m3u text file

So far I have this:

 set "texty=\Music\TheArchive\Aeph & Neonlight\Aeph & Neonlight - Space Truckers - 7b - 173.26.mp3" echo %texty% >>C:\Music\Playlists\baseplaylist.m3u 

But that does not work. I know that the problem is with the characters in the text that I am trying to copy, but I do not know how to overcome this.

+2
source share
2 answers

The problem is with the & operator, in which case you will need to expand the variable or change the operator or include the string β€œecho” with two double quotes ... But the solution in your case is so simple:

 set "texty=\Music\TheArchive\Aeph & Neonlight\Aeph & Neonlight - Space Truckers - 7b - 173.26.mp3" <nul Set /P "string=%TEXTY%" >>C:\Music\Playlists\baseplaylist.m3u 

PS : using SETLOCAL ENABLEDELAYEDEXPANSION is bad practice when you play with file names (especially when they contain a character ! ), So try to never use the extension, all this can be done without the vars (ALL) extension, which allows delayedexpansion - it's simple and a bad way to do almost everything, but not the only way.

Hope this helps you.

0
source

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.

+2
source

Source: https://habr.com/ru/post/1383762/


All Articles