BAT file: Open new cmd window and execute a command in there

后端 未结 8 2072
囚心锁ツ
囚心锁ツ 2020-11-29 15:52

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 comman

相关标签:
8条回答
  • 2020-11-29 16:00

    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.

    0 讨论(0)
  • 2020-11-29 16:01

    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.

    0 讨论(0)
  • 2020-11-29 16:01

    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

    0 讨论(0)
  • 2020-11-29 16:13

    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.

    0 讨论(0)
  • 2020-11-29 16:17

    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"
    
    0 讨论(0)
  • 2020-11-29 16:18

    Use the following in your batch file:

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

    or

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

    /c Carries out the command specified by string and then terminates
    /k Carries out the command specified by string but remains

    Consult the cmd.exe documentation using cmd /? for more details.

    The proper formating of the command string gets a little more complicated with spaces in the arguments. See the examples below. Note the use of 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 long filename:
    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

    0 讨论(0)
提交回复
热议问题