2

I have a batch file that I've thrown together in order to automate the launching of a program and several associated programs in a single click. The part relevant to the question:

echo Starting FireFox with Elite Tabset...
cd "C:\Program Files\Mozilla Firefox\"
start firefox.exe ^
https://inara.cz/cmdr/276665/ https://coriolis.io/ ^
https://www.edsm.net/ ^
http://www.elitedangeroustrading.com/trade-assistant.aspx ^
https://www.edtutorials.com/ ^
"file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Control packs/Singularity (Elite)/Singularity reference guide.pdf" "file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Companion packs/Ships assistant Ad-Astra/Ad-Astra reference guide.pdf" "file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/.Voice command lists/Elite Dangerous Voice Commands/Singularity ship commands.html" "file:///C:/Users/Guillermo/Downloads/ebonym-t16000mfcs.jpg"
echo Starting EDDiscovery...

As you can see, I've used the ^ character to break the start firefox command into multiple lines for readability's sake. This file, as shown, works. However, note the 2nd-to-last line, which starts with "file:///". There are multiple parameters on that line. What I tried to do is:

echo Starting FireFox with Elite Tabset...
cd "C:\Program Files\Mozilla Firefox\"
start firefox.exe ^
https://inara.cz/cmdr/276665/ https://coriolis.io/ ^
https://www.edsm.net/ ^
http://www.elitedangeroustrading.com/trade-assistant.aspx ^
https://www.edtutorials.com/ ^
"file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Control packs/Singularity (Elite)/Singularity reference guide.pdf" ^
"file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Companion packs/Ships assistant Ad-Astra/Ad-Astra reference guide.pdf" ^
"file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/.Voice command lists/Elite Dangerous Voice Commands/Singularity ship commands.html" ^
"file:///C:/Users/Guillermo/Downloads/ebonym-t16000mfcs.jpg"
echo Starting EDDiscovery...

When I do that, I get the below output:

C:\Program Files (x86)\VoiceAttack>echo Starting FireFox with Elite Tabset...
Starting FireFox with Elite Tabset...

C:\Program Files (x86)\VoiceAttack>cd "C:\Program Files\Mozilla Firefox\"

C:\Program Files\Mozilla Firefox>start firefox.exe https://inara.cz/cmdr/276665/ >https://coriolis.io/ https://www.edsm.net/ http://www.elitedangeroustrading.com/trade-assistant.aspx https://www.edtutorials.com/ "file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Control packs/Singularity (Elite)/Singularity reference guide.pdf" ^


C:\Program Files\Mozilla Firefox>"file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/Links and docs/Companion packs/Ships assistant Ad-Astra/Ad-Astra reference guide.pdf" "file:///C:/Program Files (x86)/VoiceAttack/Sounds/HCS Tools/.Voice command lists/Elite Dangerous Voice Commands/Singularity ship commands.html" ^
The filename, directory name, or volume label syntax is incorrect.

C:\Program Files\Mozilla Firefox>"file:///C:/Users/Guillermo/Downloads/ebonym-t16000mfcs.jpg"
The filename, directory name, or volume label syntax is incorrect.

C:\Program Files\Mozilla Firefox>echo Starting EDDiscovery... 
Starting EDDiscovery...

I don't understand why I am able to separate the first 4 arguments out, but not the ones that start with "file://. Is there some subtle facet of the ^ I am missing or mis-using? Is there a different technique for this kind of cleanup? I acknowledge that it is purely a readability concern, but it is vexing to not be able to explain this behavior.

oguz ismail
  • 1
  • 16
  • 47
  • 69
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
  • Have you thought about doublequoting all of the addresses, as opposed to those you know have spaces? I would say it was good practice. – Compo Jan 13 '21 at 00:12
  • Maybe related: [Why CMD escape character does magic instead of escaping new line](https://stackoverflow.com/questions/35309545/why-cmd-escape-character-does-magic-instead-of-escaping-new-line). – dxiv Jan 13 '21 at 00:12

1 Answers1

4

The problem are lines beginning with a double quote, because a caret at a line end escapes the line end AND the first character of the next line.
That results in escaping the first quote and the "closing" quote at the end of line is interpreted as an "opening" quote, instead.

This can be solved by simply prefix each line with a space.

start firefox.exe ^
https://www.edsm.net/ ^
https://www.edtutorials.com/ ^
 "file:///C:/Program Files/guide.pdf" ^
 "file:///C:/Program Files/blapdf" ^
...

There exists a second, more obscure solution for the problem, adding redirects in front of the carets.
With this approach the first character of the next line works as expected (will not be escaped)

start firefox.exe ^
https://www.edsm.net/ <nul ^
https://www.edtutorials.com/ <nul ^
"file:///C:/Program Files/guide.pdf" <nul ^
"file:///C:/Program Files/blapdf" <nul ^
...

More at SO:Long commands split over multiple lines..

jeb
  • 78,592
  • 17
  • 171
  • 225