通过Windows批处理文件在多行中拆分长命令

时间:2008-09-16 03:04:34

标签: batch-file

如何在批处理文件中的多行上拆分长命令?

7 个答案:

答案 0 :(得分:798)

只要您记住插入符号及其后面的换行符已完全删除,您就可以使用插入符号^分隔长行。因此,如果应该有一个空间,你要打破线,包括一个空格。 More on that below.

示例:

copy file1.txt file2.txt

将写成:

copy file1.txt^
 file2.txt

答案 1 :(得分:240)

插入符号的规则是:

在行尾的插入符号,附加下一行,附加行的第一个字符将被转义。

您可以多次使用插入符号,但完整的行不得超过〜8192个字符的最大行长度(Windows XP,Windows Vista和Windows 7)。

echo Test1
echo one ^
two ^
three ^
four^
*
--- Output ---
Test1
one two three four*

echo Test2
echo one & echo two
--- Output ---
Test2
one
two

echo Test3
echo one & ^
echo two
--- Output ---
Test3
one
two

echo Test4
echo one ^
& echo two
--- Output ---
Test4
one & echo two

要禁止转义下一个字符,可以使用重定向。

重定向必须在插入符号之前。 但是在插入符号之前存在一个重定向的好奇心。

如果您在插入符号处放置一个标记,则该标记将被删除。

echo Test5
echo one <nul ^
& echo two
--- Output ---
Test5
one
two


echo Test6
echo one <nul ThisTokenIsLost^
& echo two
--- Output ---
Test6
one
two

还可以嵌入换行符到字符串中:

setlocal EnableDelayedExpansion
set text=This creates ^

a line feed
echo Test7: %text%
echo Test8: !text!
--- Output ---
Test7: This creates
Test8: This creates
a line feed

空行对成功至关重要。这仅适用于延迟扩展,否则在换行后将忽略该行的其余部分。

它有效,因为行尾的插入符号忽略下一个换行符并转义下一个字符,即使下一个字符也是换行符(在此阶段总是忽略回车符。)

答案 2 :(得分:59)

(这基本上是对Wayne's answer的重写,但随着插入符号的混乱被清除。所以我把它作为CW发布。我并不羞于编辑答案,但完全重写它们似乎不合适。)

您可以使用插入符号(^)分隔长行,只需记住插入符号及其后面的换行符将从命令中完全删除,因此如果您将其放入在需要空间的地方(例如参数之间),一定要包括空格(在^之前,或在下一行的开头 - 后一个选择可能有助于使它更清晰它是一个续)。

示例:(全部在Windows XP和Windows 7上测试)

xcopy file1.txt file2.txt

可以写成:

xcopy^
 file1.txt^
 file2.txt

xcopy ^
file1.txt ^
file2.txt

甚至

xc^
opy ^
file1.txt ^
file2.txt

(最后一步是因为xc^之间没有空格,而下一行的开头没有空格。所以当你删除^时换行,你得到...... xcopy。)

为了便于阅读和理智,最好只在参数之间进行分解(确保包含空格)。

确保^ 批处理文件中的最后一件事,与appears to be a major issue with that一样。

答案 3 :(得分:14)

Multiple commands can be put in parenthesis and spread over numerous lines; so something like echo hi && echo hello can be put like this:

( echo hi
  echo hello )

Also variables can help:

set AFILEPATH="C:\SOME\LONG\PATH\TO\A\FILE"
if exist %AFILEPATH% (
  start "" /b %AFILEPATH% -option C:\PATH\TO\SETTING...
) else (
...

Also I noticed with carets (^) that the if conditionals liked them to follow only if a space was present:

if exist ^

答案 4 :(得分:8)

然而,似乎在for循环的值的中间分割并不需要插入符(实际上尝试使用一个将被视为语法错误)。例如,

for %n in (hello
bye) do echo %n

请注意,在你好或再见之前甚至不需要空格。

答案 5 :(得分:3)

尽管carret是实现此目的的首选方法,但这是使用宏的另一种方法,该宏通过传递的参数构造命令:

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set "{{=setlocal enableDelayedExpansion&for %%a in (" & set "}}="::end::" ) do if "%%~a" neq "::end::" (set command=!command! %%a) else (call !command! & endlocal)"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

%{{%
    echo
    "command"
    written
    on a
    few lines
%}}%

命令更容易阅读,无需插入符号,但使用特殊符号例如括号,重定向等会破坏它。因此,您可以在更简单的情况下使用。尽管您仍然可以将参数括在双引号中

答案 6 :(得分:0)

在搜索“如何拆分长 DOS 批处理文件行”时我没有找到的一件事是如何拆分包含长引用文本的内容。 事实上,上面的答案已经涵盖了它,但并不明显。使用 Caret 来逃避它们。 例如

myprog "needs this to be quoted"

可以写成:

myprog ^"needs this ^
to be quoted^"

但要注意在用脱字符结束一行之后用脱字符开始一行 - 因为它会作为脱字符出现..?:

echo ^"^
needs this ^
to be quoted^
^"

->“需要引用这个^”

相关问题