windows cmd.exe /k 开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8713960/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:04:33  来源:igfitidea点击:

cmd.exe /k switch

windowscommand-linecmd

提问by Mike

I am trying to switch to a directory using cmd and then execute a batch file

我正在尝试使用 cmd 切换到目录,然后执行批处理文件

e.g.

例如

cmd /k cd "C:\myfolder"
startbatch.bat

I have also tried (without success)

我也试过(没有成功)

cmd cd /k cd "C:\myfolder" | startbatch.bat

Although the first line (cmd /k) seems to run ok, but the second command is never run. I am using Vista as the OS

虽然第一行 (cmd /k) 似乎运行正常,但第二个命令从未运行。我使用 Vista 作为操作系统

回答by Sedat Kapanoglu

Correct syntax is:

正确的语法是:

cmd /k "cd c:\myfolder & startbatch.bat"

回答by MBu

ssg already posted correct answer. I would only add /d switch to cdcommand (eg. cd /d drive:\directory). This ensures the command works in case current directory is on different drive than the directory you want to cd to.

ssg 已经发布了正确答案。我只会将 /d 开关添加到cd命令(例如cd /d drive:\directory)。这可确保命令在当前目录与您要 cd 到的目录位于不同驱动器上的情况下起作用。

回答by nothrow

cmd cd /k "cd C:\myfolder; startbatch.bat"

or, why don't you run cmd /k c:\myfolder\startbatch.bat, and do cd c:\myfolderin the .bat file?

或者,为什么不运行cmd /k c:\myfolder\startbatch.bat, 并cd c:\myfolder在 .bat 文件中执行?

回答by RaffaelNagel

You can use &or &&as commands separator in Windows.

您可以在 Windows 中使用&&&作为命令分隔符。

Example:

例子:

cmd cd /K "cd C:\myfolder && startbatch.bat"

回答by dtasev

I can't see an answer addressing this, so if anyone needs to access a directory that has space in its name, you can add additional quotes, for example

我看不到解决这个问题的答案,因此如果有人需要访问名称中有空格的目录,您可以添加额外的引号,例如

cmd.exe /K """C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Auxiliary\Build\vcvars64.bat"" & powershell.exe"


From PowerShell you need to escape the quotes using the backquote `

在 PowerShell 中,您需要使用反引号 ` 来转义引号

cmd.exe /K "`"C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Auxiliary\Build\vcvars64.bat`" & powershell.exe"

Notice the escaped quotes

注意转义的引号

`"

inside the path string:

在路径字符串内:

"`"C:\my path\`""

This will execute the proper command in cmd, i.e. the path surrounded with quotes which should work.

这将在 中执行正确的命令cmd,即用应该工作的引号包围的路径。

The example command above will initialise the MSVC developer command prompt and go back to PowerShell, inheriting the environment and giving access to the MSVC tools.

上面的示例命令将初始化 MSVC 开发人员命令提示符并返回到 PowerShell,继承环境并授予对 MSVC 工具的访问权限。

回答by Knut Gjerden

I give this as an answer because I saw this question in a comment and cannot comment yet.

我给出这个答案是因为我在评论中看到了这个问题,但还不能发表评论。

cmd /k "cd c:\myfolder & startbatch.bat"

cmd /k "cd c:\myfolder & startbatch.bat"

works, and if you have spaces:

有效,如果你有空格:

cmd /k "cd "c:\myfolder" & startbatch.bat"

cmd /k "cd "c:\myfolder" & startbatch.bat"

As I understand it, the command is passed to cmd as "cd "c:\myfolder" & startbatch.bat", which is then broken down into cd "c:\myfolder"&startbatch.batat which point the remaining " "takes care of the path as string.

据我了解,该命令作为 cmd 传递给 cmd "cd "c:\myfolder" & startbatch.bat",然后将其分解为cd "c:\myfolder"&startbatch.bat其余" "部分作为字符串处理路径。

You can also use &&, |and ||depending on what you want to achieve.

您也可以使用&&,|||取决于您想要实现的目标。