简体   繁体   English

在提示符下编写批处理Windows命令但不执行

[英]batch windows command written in prompt but no execution

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). 为了避免重复执行太多任务,我正在设置一个批处理文件(在WINDOWS 10中)。 It opens several CMD PROMPT to a specific Directory and launch a command. 它会向特定目录打开多个CMD PROMPT并启动命令。

For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. 在一种情况下,我希望打开CMD PROMPT,转到特定目录并在不启动的情况下在PROMPT中设置COMMAND。 Then I'd just have to click on ENTER to launch that command whenever I want later on. 然后,只要以后需要,我只需单击ENTER即可启动该命令。

Here is my code: 这是我的代码:

setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"

With this code it launches the command "cordova build android". 使用此代码,它将启动命令“ cordova build android”。

If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\\MyPath>", I'd like to write: "cordova build android" behind it without launching the command. 如果我使用start cmd /k "cd /d %JCACordovaProjPath%而不是start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" 它将为我提供PROMPT:” C:\\ MyPath>“,我d想在其后写:“ cordova build android”,而无需启动命令。

Any idea? 任何想法?

Would something like this do you: 您会这​​样吗?

@Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"

Below is an alternative which may allow for your alternative double-quoting method: 下面是一个替代方法,它可能允许您使用替代双引号方法:

@Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option: 为了提供可重复执行(如注释中所述),您可以使用“ quit”选项将相关命令置于循环中:

@Echo Off
    setlocal
    Set "CordovaProjPath=C:\MyPath"
    Set "CommandToRun=cordova build android"
:loop
    Cd /D %CordovaProjPath%
    Echo %CommandToRun%
    set QUIT=
    set /p QUIT=Press ENTER to run command or 'Q' to quit:
    if /i "%QUIT%" == "Q" goto :eof
    %CommandToRun%
    goto :loop

Unlike the original, this runs the target command in the same command-window as the repeating loop. 与原始命令不同,它在与重复循环相同的命令窗口中运行目标命令。 Depending on what the command in question does, this may be more attractive (less windows popping-up). 根据相关命令的作用,这可能会更具吸引力(更少的窗口弹出)。 However, some commands may cause the main window to close; 但是,某些命令可能会导致主窗口关闭。 if this is the case, you can revert to running the command in its own window in one of two different ways. 在这种情况下,您可以使用两种不同的方法之一恢复在其自己的窗口中运行命令。 In each case, replace the line: 在每种情况下,请替换以下行:

    ...
    %CommandToRun%
    ...

Run in own window and remain open 在自己的窗口中运行并保持打开状态

    ...
    start "%CommandToRun%" /wait cmd /k %CommandToRun%
    ...

Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause. 使用/k将在目标命令运行后使命令提示符窗口保持打开状态-如果您需要查看命令的输出并且它没有自己的暂停,这可能是合适的。

Run in own window then close 在自己的窗口中运行,然后关闭

    ...
    start "%CommandToRun%" /wait cmd /c %CommandToRun%
    ...

Using /c will mean the command-prompt will close after the target command has run. 使用/c表示命令提示符将在目标命令运行后关闭。 This may be appropriate if you do not need to see the output of the command, or if it has its own pause. 如果您不需要查看命令的输出,或者有自己的暂停,这可能是合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM