加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 带双引号的批处理文件多行命令

发布时间:2020-12-14 01:44:13 所属栏目:Windows 来源:网络整理
导读:使用^符号输入带有参数的多行命令时使用双引号来使用带空格的字符串^符号也会被传递,有人可以解释这是什么方式吗? working.cmd @echo off call openfiles.cmd ^C:dirfilename.txt ^C:another_diranother_file.txt notworking.cmd @echo off call openfi
使用^符号输入带有参数的多行命令时使用双引号来使用带空格的字符串^符号也会被传递,有人可以解释这是什么方式吗?

working.cmd

@echo off 
call openfiles.cmd ^
C:dirfilename.txt ^
C:another_diranother_file.txt

notworking.cmd

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

openfiles.cmd看起来像

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

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

)

pause

我得到的错误

C:&;call openfiles.cmd "C:dir with spacesfile with spaces.txt" ^
ELSE was unexpected at this time.
问题是,第二行开头的引号将被多行插入符号转义.
因此,行中的最后一个引号开始引用而不是停止引用,因此第二行中的插入符号作为普通字符处理.
call openfiles.cmd ^"C:dir with spacesfile with spaces.txt" ^
**This is a seperate line** "C:another dir with spacesanother file with spaces.txt"

插入符号规则:

插入符号逃脱了下一个角色,因此角色会失去所有特效.
如果下一个字符是换行符,则删除此字符并取下一个字符(即使这也是换行符).

通过这个简单的规则,你可以解释像

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

setlocal EnableDelayedExpansion
set linefeed=^


echo #4 line1!linefeed!line2

#3创建了一个名为“Cat Dog”的文件,因为空间已被转义,不再作为分隔符.

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

echo #5 Line1< nul ^
& echo Line2

所以你也可以使用它来构建你的多线命令

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

或者使用宏

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读