cmd.exe /k 开关
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正确的语法是:
Correct syntax is:
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.或者,为什么不运行
cmd /kc:\myfolder\startbatch.bat
,并在 .bat 文件中执行cd c:\myfolder
?or, why don't you run
cmd /k c:\myfolder\startbatch.bat
, and docd c:\myfolder
in the .bat file?我看不到解决此问题的答案,因此,如果有人需要访问名称中包含空格的目录,您可以添加其他引号,例如,
从 PowerShell 中,您需要使用反引号来转义引号 `
转义的引号
注意内部 路径字符串:
这将在 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
From PowerShell you need to escape the quotes using the backquote `
Notice the escaped quotes
inside the path string:
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.
您可以使用
&
或&&
作为 Windows 中的命令分隔符。例子:
You can use
&
or&&
as commands separator in Windows.Example:
我给出这个答案是因为我在评论中看到了这个问题,但还不能发表评论。
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 intocd "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.对于任务计划程序:
cmd
/k somecommand "C:\Users\SomeUser\Some Folder With Spaces" --some-argument-for-somecommand
将
/k
替换为/c
以在完成后终止cmd
For Task Scheduler:
cmd
/k somecommand "C:\Users\SomeUser\Some Folder With Spaces" --some-argument-for-somecommand
Replace
/k
with/c
to terminatecmd
upon completion