cmd.exe /k 开关

发布于 2024-12-25 04:05:44 字数 293 浏览 1 评论 0原文

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

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

我也尝试过(没有成功)

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

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

这样做的正确方法是什么?

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

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 okay, but the second command is never run. I am using Vista as the OS.

What is the correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岁吢 2025-01-01 04:05:44

正确的语法是:

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

Correct syntax is:

cmd /k "cd /d c:\myfolder && startbatch.bat"
风尘浪孓 2025-01-01 04:05:44

ssg 已经发布了正确答案。我只会将 /d 开关添加到 cd 命令(例如 cd /ddrive:\directory)。这可以确保该命令在当前目录与您想要 cd 到的目录位于不同驱动器上时有效。

ssg already posted correct answer. I would only add /d switch to cd command (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.

寄风 2025-01-01 04:05:44
cmd cd /k "cd C:\myfolder; startbatch.bat"

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

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

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

一世旳自豪 2025-01-01 04:05:44

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

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

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

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

转义的引号

`"

注意内部 路径字符串:

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

这将在 cmd 中执行正确的命令,即用引号括起来的路径应该可以工作。

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

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 Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"" & powershell.exe"

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

cmd.exe /K "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\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.

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.

你在看孤独的风景 2025-01-01 04:05:44

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

例子:

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

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

Example:

cmd cd /K "cd C:\myfolder && startbatch.bat"
心碎的声音 2025-01-01 04:05:44

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

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

有效,如果有空格:

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

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

您还可以根据您想要实现的目标使用 &&|||

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"

works, and if you have spaces:

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.bat at which point the remaining " " takes care of the path as string.

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

荒路情人 2025-01-01 04:05:44

对于任务计划程序:

设置
程序/脚本cmd
添加参数(可选)/k somecommand "C:\Users\SomeUser\Some Folder With Spaces" --some-argument-for-somecommand

/k 替换为 /c 以在完成后终止 cmd

For Task Scheduler:

SettingValue
Program/scriptcmd
Add arguments (optional)/k somecommand "C:\Users\SomeUser\Some Folder With Spaces" --some-argument-for-somecommand

Replace /k with /c to terminate cmd upon completion

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文