BAT 文件:打开新的 cmd 窗口并在其中执行命令

发布于 2025-01-08 03:52:16 字数 191 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(11

ˇ宁静的妩媚 2025-01-15 03:52:16

您可能已经找到了答案,因为您不久前就问过。但我在编码错误时尝试做类似的事情。我想在新的 cmd 窗口中运行“rails server”,这样我就不必打开新的 cmd,然后再次找到我的路径。

我发现使用 K 开关是这样的:

start cmd /k echo Hello, World!

在“cmd”之前启动将在新窗口中打开应用程序,而“/K”将执行“echo Hello, World!”新的cmd启动后。

您还可以使用 /C 开关进行类似的操作。

start cmd /C pause

然后,这将执行“暂停”,但在命令完成后关闭窗口。在这种情况下,在您按下按钮后。我发现这对于“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 cmd /k echo Hello, World!

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.

start cmd /C pause

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.

北座城市 2025-01-15 03:52:16

在批处理文件中使用以下内容:

start cmd.exe /c "more-batch-commands-here"

start cmd.exe /k "more-batch-commands-here"
  • /c运行命令,然后关闭终端窗口,保持桌面干净。
  • /k 运行命令,然后保持终端窗口打开。

/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:

start cmd.exe /c "more-batch-commands-here"

or

start cmd.exe /k "more-batch-commands-here"
  • /c run command then close the terminal window leaving your desktop clean.
  • /k run command then keep the terminal window open.

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

完美的未来在梦里 2025-01-15 03:52:16

上面的答案对我有帮助。但仍然需要一些弄清楚。这是我用来启动 3 个 Web 开发进程的示例脚本。这会导致 3 个窗口保持打开状态,因为它们需要连续运行。

Mongo 已全局添加到我的路径中,因此我不需要像其他两个程序那样进行 cd 操作。当然,文件的路径会有所不同,但希望这会有所帮助。

:: Start MongoDB
start cmd.exe /k "mongod"

:: cd app directory, and start it
cd my-app
start cmd.exe /k "npm run dev"

:: cd to api server, and start that
cd ../my-app-api
start cmd.exe /k "npm run dev"

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.

:: Start MongoDB
start cmd.exe /k "mongod"

:: cd app directory, and start it
cd my-app
start cmd.exe /k "npm run dev"

:: cd to api server, and start that
cd ../my-app-api
start cmd.exe /k "npm run dev"
怎会甘心 2025-01-15 03:52:16

这并不容易。

最好的方法是将要在“新窗口”中执行的脚本部分放在单独的 .bat 文件中。如果您需要脚本其余部分(变量等)的大量状态,这可能不切实际。一种选择是将您需要的任何值(例如要操作的目录)传递到批处理文件:

start cmd.exe stuff.bat %this_dir%

如果您有大量状态要传输,您可能会考虑在运行时生成批处理文件:

set foo=Hello, World
set list_me=%userprofile%

set tmpdir=c:\windows\temp
set tmp=%tmpdir%\tmp.foo

del /q /f "%tmp%"

echo.echo %foo%>>"%tmp%"
echo.dir "%list_me%">>>"%tmp"

start cmd.exe "%tmp%"

del /q /f "%tmp%"

显然这是一个简单的示例。

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:

start cmd.exe stuff.bat %this_dir%

If you have a large amount of state to transmit you might consider generating a batch file at runtime:

set foo=Hello, World
set list_me=%userprofile%

set tmpdir=c:\windows\temp
set tmp=%tmpdir%\tmp.foo

del /q /f "%tmp%"

echo.echo %foo%>>"%tmp%"
echo.dir "%list_me%">>>"%tmp"

start cmd.exe "%tmp%"

del /q /f "%tmp%"

Obviously this is a trivial example.

橘味果▽酱 2025-01-15 03:52:16

感谢 Stack Overflow 上的所有人;该解决方案解决了上述问题,但扩展为自动运行这些任务:

  1. 我想运行我的rails服务器
  2. 为我的delayed_job gem运行一个rake jobs:worker
  3. 并打开默认值 最后,互联网浏览器显示我的页面
  4. 留下一个cmd窗口打开,以在会话期间执行任何额外命令。

我猜我的项目名为“antiquorum”。

在您的%USERPROFILE%目录中创建一个“init.bat”文件(打开 >cmd 窗口并查看光标左侧的路径以了解 %USERPROFILE% 是什么)

@echo off
cd C:/projects/rails3/antiquorum
if "%1" == "antiquorum" GOTO start
if "%1" == "worker" GOTO worker
if "%1" == "server" GOTO server
if "%1" == "" GOTO end
:start
    start cmd /k %USERPROFILE%\init.bat worker
    start cmd /k %USERPROFILE%\init.bat server
    TIMEOUT 30
    start "" "http://localhost:3000/"
    GOTO end
:server
    rails s
    GOTO end
:worker
    rake jobs:work
:end

在新的命令行窗口中键入: C:> init antiquorum

代码会打开另外两个 cmd 窗口和一个浏览器。 TIMEOUT 避免浏览器中出现错误。

:start 部分完成这项工作。
您可以通过输入以下参数来单独运行任务 1,2 或 4:serverworker 或 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:

  1. I want to run my rails server
  2. Run a rake jobs:worker for my delayed_job gem too
  3. and Open default internet browser to show my page
  4. finally, to leave a cmd window open for any extra commands during my session.

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)

@echo off
cd C:/projects/rails3/antiquorum
if "%1" == "antiquorum" GOTO start
if "%1" == "worker" GOTO worker
if "%1" == "server" GOTO server
if "%1" == "" GOTO end
:start
    start cmd /k %USERPROFILE%\init.bat worker
    start cmd /k %USERPROFILE%\init.bat server
    TIMEOUT 30
    start "" "http://localhost:3000/"
    GOTO end
:server
    rails s
    GOTO end
:worker
    rake jobs:work
:end

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.

流云如水 2025-01-15 03:52:16

添加 /k 在两个命令之间 按顺序执行两个命令< /强>。

示例:

cmd /k echo "hello"

此命令将首先打开命令提示符,然后执行echo "hello"命令

Adding /k between two commands executes both command in order.

Example:

cmd /k echo "hello"

this command will first open command prompt then execute echo "hello" command

终止放荡 2025-01-15 03:52:16

如果我理解您在 bat 文件中正确执行此操作,则会打开命令提示符并将消息打印到屏幕上。

cmd.exe hello world

希望这有帮助。

If I understand you correctly doing this in side your bat file will open Command prompt and print your message to screen.

cmd.exe hello world

hope this helps.

南笙 2025-01-15 03:52:16

在新的 cmd 窗口中运行文件名中包含空格的 python 文件:

start cmd.exe /k python "C:\Program Files\HelloWorld.py"

to run a python file in a new cmd window with spaces in the file name:

start cmd.exe /k python "C:\Program Files\HelloWorld.py"
初见你 2025-01-15 03:52:16

扩展@Dan Zuzevich 的答案。如果您的目标文件位于不同的驱动器上,下面是 CD“更改目录”。例如,它必须从驱动器 C 和驱动器 D 调用文件。

:: Start MongoDB
start cmd.exe /k "mongod"

:: cd app directory, and start it
cd C:\project\dev
C:
start cmd.exe /k "npm run dev"

:: cd to api server, and start that
cd D:\api\server
D:
start cmd.exe /k "npm run dev"

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.

:: Start MongoDB
start cmd.exe /k "mongod"

:: cd app directory, and start it
cd C:\project\dev
C:
start cmd.exe /k "npm run dev"

:: cd to api server, and start that
cd D:\api\server
D:
start cmd.exe /k "npm run dev"
征﹌骨岁月お 2025-01-15 03:52:16

我需要运行一个环境,然后在该环境中使用 python。

如果我的文件夹结构是:

C:\environments>
C:\environments\encrypted_stuff>
C:\environments\encrypted_stuff\p1>
C:\environments\encrypted_stuff\p2>

并且我想激活“encrypted_stuff”环境,然后从 p2 调用脚本,我可以在存储在 C:\environments\p2.bat 中的 bat 中键入此脚本

start cmd /k "Scripts\activate && cd p2 && python p2_script.py"

然后我可以有一个 bat 文件任何脚本都将使用我的“encrypted_stuff”环境。现在我可以从桌面单击 bat 文件,它将加载我的特定环境,然后在该环境中运行脚本。

I needed to run an environment and then use python while in that environment.

If my folder structure is:

C:\environments>
C:\environments\encrypted_stuff>
C:\environments\encrypted_stuff\p1>
C:\environments\encrypted_stuff\p2>

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

start cmd /k "Scripts\activate && cd p2 && python p2_script.py"

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.

熊抱啵儿 2025-01-15 03:52:16

我希望在终止或重新启动 Firebase 后我的窗口保持打开状态,因此我使用了两个批处理文件。

方便访问的桌面文件:run_firebase.bat

--------------START FILE CONTENTS--------------

start cmd /k C:\dev\init_firebase_batch.bat

---------------END FILE CONTENTS---------------

运行预期结果的批处理文件:C:\dev\init_firebase_batch.bat

--------------START FILE CONTENTS--------------

cd C:\dev\aptr_datasync\aperture-customer-hub
firebase emulators:start

---------------END FILE CONTENTS---------------

所以...双击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:

--------------START FILE CONTENTS--------------

start cmd /k C:\dev\init_firebase_batch.bat

---------------END FILE CONTENTS---------------

Batch file to run the intended results: C:\dev\init_firebase_batch.bat

--------------START FILE CONTENTS--------------

cd C:\dev\aptr_datasync\aperture-customer-hub
firebase emulators:start

---------------END FILE CONTENTS---------------

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

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