2015-11-16 49 views
5

当使用^符号输入带引号的多行命令时,如果使用双引号来使用带空格的字符串,还会传递^符号,任何人都可以解释这是什么吗?带双引号的批处理文件多行命令

working.cmd

@echo off 
call openfiles.cmd^
C:\dir\filename.txt^
C:\another_dir\another_file.txt 

notworking.cmd

@echo off 
call openfiles.cmd^
"C:\dir with spaces\file with spaces.txt"^
"C:\another dir with spaces\another file with spaces.txt" 

openfiles.cmd貌似

@echo off 
for %%x in (%*) do (

    IF EXIST %%x (
     call "c:\Program Files\Notepad++\notepad++.exe" %%x 
    ) ELSE (
     call echo Not found %%x 
    ) 

) 

pause 

我的错误看起来像

C:\>call openfiles.cmd "C:\dir with spaces\file with spaces.txt"^
ELSE was unexpected at this time. 
+0

思在例如尝试在上面,我在前面的多余空格的双引号原来并没有它似乎修复我的问题 – Mazaka

+0

它失败了,因为报价逃脱了,因此空间可以拆分文件mes到更小的部分 – jeb

+0

“dubble”是我最喜欢的单词。 –

回答

5

插入符号规则:

脱字符转义下一个字符,使字符失去一切特殊效果。
如果下一个字符是换行符,则取下一个字符(即使这也是换行符)。

有了这个简单的规则,你能解释像

echo #1 Cat^&Dog 
echo #2 Cat^ 
&Dog 
echo #3 Redirect to > Cat^ 
Dog 

setlocal EnableDelayedExpansion 
set linefeed=^ 


echo #4 line1!linefeed!line2 

#3东西创建一个名为“猫狗”的空间被逃了出来,作为分隔符了不工作的文件。

但它仍然有可能打破这个规则!
您只需将任何重定向放在插入符前面,它仍然会丢弃换行符(多行仍然有效),但是下一个字符不会再被转义。

echo #5 Line1< nul^
& echo Line2 

所以,你也可以用它来建立自己的多命令

call openfiles.cmd < nul^
"C:\dir with spaces\file with spaces.txt" < nul^
"C:\another dir with spaces\another file with spaces.txt" 

或使用

set "\n=< nul ^" 
call openfiles.cmd %\n% 
"C:\dir with spaces\file with spaces.txt" %\n% 
"C:\another dir with spaces\another file with spaces.txt" 
1

在尝试了一些不同的事情之后,我设法让它只用双引号的额外空间。 变化notworking.cmd于以下工作

@echo off 
call openfiles.cmd^
"C:\dir with spaces\file with spaces.txt"^
"C:\another dir with spaces\another file with spaces.txt" 

注意空间在双引号前面

+0

notepad ++说我的行结尾是CR-LF当我查看 - >符号 - >显示所有字符,所以仍然不知道为什么空格帮我 – Mazaka

+0

哦,的确,我可以重现。事情是,我甚至从来没有想过使用连续行而不缩进它们(通常使用TAB)... – wOxxOm

+0

感谢您的反馈,它确实解决了我的问题。 – Mazaka