我试图在一个BAT文件中打开一个新的命令窗口:
start %windir%\system32\cmd.exe
打开新窗口后,我想在其中执行一个BAT命令:
echo "test in new window"
我该怎么做?
我试图在一个BAT文件中打开一个新的命令窗口:
start %windir%\system32\cmd.exe
打开新窗口后,我想在其中执行一个BAT命令:
echo "test in new window"
我该怎么做?
你可能已经找到了答案,因为询问时间已经过去一段时间了。但是我在编写ror代码时尝试做类似的事情。我想在一个新的命令行窗口中运行"rails server",这样我就不必打开一个新的命令行并再次查找我的路径。
我发现可以使用K开关来实现,像这样:
start cmd /k echo Hello, World!
在“cmd”之前加上“start”将会在一个新窗口中打开应用程序,“/K”会在新的cmd打开后执行“echo Hello, World!”。
您也可以使用/C开关来实现类似的功能。
start cmd /C pause
这将执行 "pause" 命令,但在命令执行完毕后关闭窗口。在这种情况下,当你按下一个按钮时。我发现对于 "rails server" 很有用,然后当我关闭我的开发服务器后,我不必再关闭窗口了。start cmd.exe /c "more-batch-commands-here"
或者
start cmd.exe /k "more-batch-commands-here"
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
程序路径中的空格 + 参数:CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
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
以上答案对我有所帮助。但是仍然需要一些解决思路。这是一个示例脚本,我用它启动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"
这并不是很容易。
最好的方法是将你想要在“新窗口”中执行的脚本部分放在一个单独的 .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%"
显然,这只是一个微不足道的例子。
@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
在两个命令之间添加/k
会按顺序执行两个命令。
例如:
cmd /k echo "hello"
该命令将首先打开命令提示符(command prompt),然后执行echo "hello"命令。
如何在新的cmd窗口中运行文件名带有空格的Python文件:
start cmd.exe /k python "C:\Program Files\HelloWorld.py"
cmd.exe hello world
延续@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"
我需要运行一个环境,然后在该环境中使用Python。
如果我的文件夹结构是:
C:\environments>
C:\environments\encrypted_stuff>
C:\environments\encrypted_stuff\p1>
C:\environments\encrypted_stuff\p2>
我想要激活"encrypted_stuff"环境,然后从p2调用一个脚本,我可以在存储在C:\environments\p2.bat的批处理文件中输入以下内容
start cmd /k "Scripts\activate && cd p2 && python p2_script.py"