Windows:如何在命令提示符下指定多行命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/605686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows: How to specify multiline command on command prompt?
提问by Jigar
how do we extend a command to next line?
我们如何将命令扩展到下一行?
basically whats the windows alternative for linux's
基本上什么是Linux的Windows替代品
ls -l \
/usr/
here we use backslash to extend command on next line
这里我们使用反斜杠来扩展下一行的命令
whats the equivalent for windows?
windows 的等价物是什么?
回答by Timbo
After trying almost every key on my keyboard:
在尝试了我键盘上的几乎每个键之后:
C:\Users\Tim>cd ^
Mehr? Desktop
C:\Users\Tim\Desktop>
So it seems to be the ^ key.
所以它似乎是 ^ 键。
回答by Daemin
In the Windows Command Prompt the ^
is used to escape the next character on the command line. (Like \
is used in strings.) Characters that need to be used in the command line as they are should have a ^ prefixed to them, hence that's why it works for the newline.
在 Windows 命令提示符中,^
用于转义命令行上的下一个字符。(Like\
在字符串中使用。)需要在命令行中使用的字符应该以 ^ 为前缀,因此这就是它适用于换行符的原因。
For reference the characters that need escaping (if specified as command arguments and not within quotes) are: &|()
作为参考,需要转义的字符(如果指定为命令参数而不是在引号内)是: &|()
So the equivalent of your linux example would be (the More?being a prompt):
因此,相当于您的 linux 示例将是(更多?作为提示):
C:\> dir ^
More? C:\Windows
回答by Ali
The caret character works, however the next line should not start with double quotes. e.g. this will not work:
插入符号有效,但下一行不应以双引号开头。例如,这将不起作用:
C:\ ^
"SampleText" ..
Start next line without double quotes (not a valid example, just to illustrate)
开始下一行没有双引号(不是一个有效的例子,只是为了说明)
回答by kayleeFrye_onDeck
If you came here looking for an answer to this question but not exactlythe way the OP meant, ie how do you get multi-line CMD to work in a singleline, I have a sort of dangerous answer for you.
如果你来到这里寻找一个回答这个问题,但没有确切的OP的意思的方式,也就是你怎么在获得多行CMD工作单行,我有一种适合你危险的答案。
Trying to use this with things that actually use piping, like say findstr
is quite problematic. The same goes for dealing with else
s. But if you just want a multi-line conditional command to execute directly from CMD and not via a batch file, this should do work well.
尝试将其与实际使用管道的事物一起使用,例如 sayfindstr
是相当有问题的。处理else
s 也是如此。但是,如果您只想直接从 CMD 执行多行条件命令,而不是通过批处理文件执行,这应该可以很好地工作。
Let's say you have something like this in a batch that you want to run directly in command prompt:
假设您希望在命令提示符下直接运行批处理中的类似内容:
@echo off
for /r %%T IN (*.*) DO (
if /i "%%~xT"==".sln" (
echo "%%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file
echo Dumping SLN file contents
type "%%~T"
)
)
Now, you could use the line-continuation carat (^
) and manually type it out like this, but warning, it's tedious and if you mess up you can learn the joy of typing it all out again.
现在,您可以使用续行符 carat ( ^
) 并像这样手动输入它,但警告,这很乏味,如果您搞砸了,您可以学习再次输入的乐趣。
Well, it won't work with just ^
thanks to escaping mechanisms inside of parentheses shrugAt least not as-written. You actually would need to double up the carats like so:
好吧,仅仅^
由于括号内的转义机制,它不会起作用至少耸耸肩。你实际上需要像这样将克拉加倍:
@echo off ^
More? for /r %T IN (*.sln) DO (^^
More? if /i "%~xT"==".sln" (^^
More? echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file^^
More? echo Dumping SLN file contents^^
More? type "%~T"))
Instead, you can be a dirty sneaky scripter from the wrong side of the tracks that don't need no carats by swapping them out for a single pipe (|
) per continuation of a loop/expression:
取而代之的是,您可以成为不需要克拉数的轨道错误一侧的肮脏鬼鬼祟祟的脚本编写者,方法|
是在循环/表达式的每个延续中将它们交换为单个管道 ( ):
@echo off
for /r %T IN (*.sln) DO if /i "%~xT"==".sln" echo "%~T" is a normal SLN file, and not a .SLN.METAPROJ or .SLN.PROJ file | echo Dumping SLN file contents | type "%~T"