wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了windows – 带双引号的批处理文件多行命令大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

使用^符号输入带有参数的多行命令时使用双引号来使用带空格的字符串^符号也会被传递,有人可以解释这是什么方式吗? working.cmd @echo off call openfiles.cmd ^ C:\dir\filename.txt ^ C:\another_dir\another_file.txt notworking.cmd @echo off call openfiles.cmd ^
使用^符号输入带有参数的多行命令时使用双引号来使用带空格的字符串^符号也会被传递,有人可以解释这是什么方式吗?

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.
问题是,第二行开头的引号将被多行插入符号转义.
因此,行中的最后一个引号开始引用而不是停止引用,因此第二行中的插入符号作为普通字符处理.
call openfiles.cmd ^"C:\dir with spaces\file with spaces.txt" ^
**This is a seperate line** "C:\another dir with spaces\another 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 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"

大佬总结

以上是大佬教程为你收集整理的windows – 带双引号的批处理文件多行命令全部内容,希望文章能够帮你解决windows – 带双引号的批处理文件多行命令所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。