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

后端 未结 8 2152
囚心锁ツ
囚心锁ツ 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: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.

提交回复
热议问题