当使用^符号input带有参数的多行命令时,如果使用双引号来使用带空格的string,^符号也会被传递,任何人都可以解释这是什么方法吗?
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.
一个脱字符逃脱下一个字符,使字符失去所有特殊效果。
如果下一个字符是换行符,则取下一个字符(即使这也是换行符)。
有了这个简单的规则,你可以解释一些事情
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"
在尝试了一些不同的东西之后,我设法使它只用双引号的额外空间。 更改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"
注意双引号前面的空格