windows - 使用 `^` 转义 Windows 批处理文件中的行尾字符在某些行上不起作用?

标签 windows batch-file cmd command-line

我有一个批处理文件,我将其放在一起,以便通过单击自动启动一个程序和多个关联程序。与问题相关的部分:

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...

如您所见,我使用了 ^字符打破start firefox为了便于阅读,将命令分成多行。如图所示,该文件可以工作。但是,请注意倒数第二行,它以“file:/// ”开头。该行有多个参数。我试图做的是:

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...

当我这样做时,我得到以下输出:

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...

我不明白为什么我能够分离出前 4 个参数,但不能分离出以 "file:// 开头的参数。 。 ^ 有一些微妙的方面吗?是我遗漏了还是误用了?这种清理有不同的技术吗?我承认这纯粹是一个可读性问题,但无法解释这种行为是令人烦恼的。

最佳答案

问题是以双引号开头的行,因为行尾处的插入符号会转义行尾和下一行的第一个字符。
这会导致转义第一个引号,而行末尾的“结束”引号会被解释为“开始”引号。

只需在每行前面加上空格即可解决此问题。

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

对于这个问题,还有第二种更晦涩的解决方案,即在插入符号前面添加重定向。
通过这种方法,下一行的第一个字符将按预期工作(不会被转义)

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 ^
...

更多信息请访问SO:Long commands split over multiple lines..

关于windows - 使用 `^` 转义 Windows 批处理文件中的行尾字符在某些行上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65693417/

相关文章:

windows - 如何在不手动卸载旧版本的情况下升级应用程序的最新版本?

sql - 使用DDD,如何实现批处理?

windows-7 - 来自bat 文件的Mysqldump 问题

windows - 从多词字符串的每个词的第一个字符创建新字符串

java - 如何在 Windows Pro 中从命令行运行应用程序?

python - Windows 10 和无法找到 vcvarsall.bat

启动时 ucrtbase.dll 上的 Windows C++ 服务错误

c++ - 创建qt线程事件循环

windows - 在 Windows 中批量重命名多个文件?

batch-file - 在 Windows 批处理文件中读取 "for"命令中的空值