BAT 文件:打开新的 cmd 窗口并在其中执行命令
我正在尝试在 BAT 文件中打开一个新的命令窗口:
start %windir%\system32\cmd.exe
打开后,我想在新窗口中执行 BAT 命令:
echo "test in new window"
我该如何执行此操作?
I'm trying to open a new command window in a BAT file:
start %windir%\system32\cmd.exe
After it opens, I'd like to execute a BAT command in the new window:
echo "test in new window"
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可能已经找到了答案,因为您不久前就问过。但我在编码错误时尝试做类似的事情。我想在新的 cmd 窗口中运行“rails server”,这样我就不必打开新的 cmd,然后再次找到我的路径。
我发现使用 K 开关是这样的:
在“cmd”之前启动将在新窗口中打开应用程序,而“/K”将执行“echo Hello, World!”新的cmd启动后。
您还可以使用 /C 开关进行类似的操作。
然后,这将执行“暂停”,但在命令完成后关闭窗口。在这种情况下,在您按下按钮后。我发现这对于“rails 服务器”很有用,然后当我关闭我的开发服务器时,我不必关闭窗口。
You may already find your answer because it was some time ago you asked. But I tried to do something similar when coding ror. I wanted to run "rails server" in a new cmd window so I don't have to open a new cmd and then find my path again.
What I found out was to use the K switch like this:
start before "cmd" will open the application in a new window and "/K" will execute "echo Hello, World!" after the new cmd is up.
You can also use the /C switch for something similar.
This will then execute "pause" but close the window when the command is done. In this case after you pressed a button. I found this useful for "rails server", then when I shutdown my dev server I don't have to close the window after.
在批处理文件中使用以下内容:
或
/c
和/k
选项控制命令完成运行后发生的情况。使用
cmd /?
查阅 cmd.exe 文档以了解更多详细信息。使用空格转义命令
当使用带空格的参数时,命令字符串的正确格式会变得更加复杂。请参阅下面的示例。请注意某些示例中的嵌套双引号。
示例:
运行程序并传递文件名参数:
CMD /c write.exe c:\docs\sample.txt
运行程序并传递包含空格的文件名:
CMD /c write.exe "c:\sampledocuments\sample.txt"
程序路径中的空格:
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
程序路径 + 参数中的空格:
CMD /c ""c:\Program Files\demo.cmd"" 参数1参数2
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
启动 demo1 和 demo2:
CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""
来源: http://ss64.com/nt/cmd.html
Use the following in your batch file:
or
The
/c
and/k
options controls what happens once your command finishes running.Consult the cmd.exe documentation using
cmd /?
for more details.Escaping Commands with White Spaces
The proper formatting of the command string becomes more complicated when using arguments with spaces. See the examples below. Note the nested double quotes in some examples.
Examples:
Run a program and pass a filename parameter:
CMD /c write.exe c:\docs\sample.txt
Run a program and pass a filename which contains whitespace:
CMD /c write.exe "c:\sample documents\sample.txt"
Spaces in program path:
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
Spaces in program path + parameters:
CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
Launch demo1 and demo2:
CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""
Source: http://ss64.com/nt/cmd.html
上面的答案对我有帮助。但仍然需要一些弄清楚。这是我用来启动 3 个 Web 开发进程的示例脚本。这会导致 3 个窗口保持打开状态,因为它们需要连续运行。
Mongo 已全局添加到我的路径中,因此我不需要像其他两个程序那样进行 cd 操作。当然,文件的路径会有所不同,但希望这会有所帮助。
The above answers helped me. But still required some figuring out. Here is an example script I use to start 3 processes for web development. It results in 3 windows staying open, as they need to run continously.
Mongo is globally added to my path, so I don't need to cd like I do for the other two programs. Of course the path to your files will vary, but hopefully this will help.
这并不容易。
最好的方法是将要在“新窗口”中执行的脚本部分放在单独的 .bat 文件中。如果您需要脚本其余部分(变量等)的大量状态,这可能不切实际。一种选择是将您需要的任何值(例如要操作的目录)传递到批处理文件:
如果您有大量状态要传输,您可能会考虑在运行时生成批处理文件:
显然这是一个简单的示例。
This is not very easy.
The best approach is to have the part of your script that you want to be executed in a "new window" to be in a separate .bat file. This might be impractical if e.g. you need a lot of state from the rest of your script (variables, etc). One option is to pass any values you need (e.g. dir to operate in) to the batch file:
If you have a large amount of state to transmit you might consider generating a batch file at runtime:
Obviously this is a trivial example.
感谢 Stack Overflow 上的所有人;该解决方案解决了上述问题,但扩展为自动运行这些任务:
我猜我的项目名为“antiquorum”。
在您的%USERPROFILE%目录中创建一个“init.bat”文件(打开 >cmd 窗口并查看光标左侧的路径以了解 %USERPROFILE% 是什么)
在新的命令行窗口中键入: C:> init antiquorum
代码会打开另外两个 cmd 窗口和一个浏览器。 TIMEOUT 避免浏览器中出现错误。
:start 部分完成这项工作。
您可以通过输入以下参数来单独运行任务 1,2 或 4:server、worker 或 none 以使 cmd 在 root 中打开“antiquorum”项目。
享受。
Thanks to all here in Stack Overflow; this solution solves the above question but is extended to automatically run these tasks:
I guess my project is called "antiquorum."
Create an "init.bat" file in your %USERPROFILE% directory (open a cmd window and take a look at the path to the left of the cursor to know what %USERPROFILE% is)
In a new command line window type: C:> init antiquorum
The code opens two more cmd windows and a browser. TIMEOUT avoids errors in the browser.
The :start section does the work.
You can run tasks 1,2 or 4 separately by typing params as: server, worker, or none to leave a cmd opened in root of "antiquorum" project.
Enjoy.
添加
/k
在两个命令之间 按顺序执行两个命令< /强>。示例:
此命令将首先打开
命令提示符
,然后执行echo "hello"
命令Adding
/k
between two commands executes both commandin order
.Example:
this command will first open
command prompt
then executeecho "hello"
command如果我理解您在 bat 文件中正确执行此操作,则会打开命令提示符并将消息打印到屏幕上。
希望这有帮助。
If I understand you correctly doing this in side your bat file will open Command prompt and print your message to screen.
hope this helps.
在新的 cmd 窗口中运行文件名中包含空格的 python 文件:
to run a python file in a new cmd window with spaces in the file name:
扩展@Dan Zuzevich 的答案。如果您的目标文件位于不同的驱动器上,下面是 CD“更改目录”。例如,它必须从驱动器 C 和驱动器 D 调用文件。
Extending answer from @Dan Zuzevich. Below is CD "Change Directory" if your target file is on different drive. For example it have to call file from drive C and drive D.
我需要运行一个环境,然后在该环境中使用 python。
如果我的文件夹结构是:
并且我想激活“encrypted_stuff”环境,然后从 p2 调用脚本,我可以在存储在 C:\environments\p2.bat 中的 bat 中键入此脚本
然后我可以有一个 bat 文件任何脚本都将使用我的“encrypted_stuff”环境。现在我可以从桌面单击 bat 文件,它将加载我的特定环境,然后在该环境中运行脚本。
I needed to run an environment and then use python while in that environment.
If my folder structure is:
And I wanted to activate the "encrypted_stuff" environment then call a script from p2, I might type this in a bat that is stored in C:\environments\p2.bat
Then I can have a bat file for any of the scripts and all would use my "encrypted_stuff" environment. Now I can click the bat file from the desktop and it will load my specific environment and then run the script in that environment.
我希望在终止或重新启动 Firebase 后我的窗口保持打开状态,因此我使用了两个批处理文件。
方便访问的桌面文件:
run_firebase.bat
:运行预期结果的批处理文件:
C:\dev\init_firebase_batch.bat
所以...双击
run_firebase .bat
并运行第二批。在 Ctrl+C 退出我的进程后,窗口保持打开状态。看起来微不足道,但我不喜欢在编写代码时分心。我只花时间解决,因为我认为这很简单。希望这能为其他人带来这种简单性。
我认为这检查了最初的帖子中的问题:
[x]我正在尝试在 BAT 文件中打开一个新的命令窗口
[x] 打开后,我想在新窗口中执行BAT命令
I wanted my window to remain open after I killed or restarted Firebase so I used two batch files.
Desktop file for easy access:
run_firebase.bat
:Batch file to run the intended results:
C:\dev\init_firebase_batch.bat
So ... double click
run_firebase.bat
and it runs the second batch. After Ctrl+C out of my process the window remains open. Seems trivial but I don't like distractions while I'm working on code.I only spent time solving because I thought it would be simple. Hopefully this creates that simplicity for others.
I think this checks off the questions in the initial post:
[x] I'm trying to open a new command window in a BAT file
[x] After it opens, I'd like to execute a BAT command in the new window