Several ways to call a windows batch file from another one or from prompt. Which one in which case?

前端 未结 2 452
滥情空心
滥情空心 2020-11-30 21:22

A windows batch file (called.bat or called.cmd) can be called from another batch file (caller.bat or caller.cmd) or inter

相关标签:
2条回答
  • 2020-11-30 21:31
    1. The batch file will be executed by the current cmd.exe instance (or a new cmd.exe instance if, for instance, double-clicked in Explorer).

    2. Same as #1, only has an effect when used inside a batch/cmd file. In a batch file, without 'call', the parent batch file ends and control passes to the called batch file; with 'call' runs the child batch file, and the parent batch file continues with statements following call.

    3. Runs the batch file in a new cmd.exe instance.

    4. Start will run the batch file in a new cmd.exe instance in a new window, and the caller will not wait for completion.

    0 讨论(0)
  • 2020-11-30 21:40

    One thing not clear from the comments here: When you call one batch file from another by using just its name (Case #1 in the original question), execution stops from the calling batch file. For example, in these lines:

    called.bat
    echo Hello
    

    The 'echo Hello' line (and anything following it) will not be called. If you use the 'call' keyword, execution resumes after the call. So in this case:

    call called.bat
    echo Hello
    

    The 'echo Hello' line will be called.

    Additionally, all the variables set in the called.bat file will be passed along back to the calling process, too.

    Imagine a 'called.bat' file that had this line:

    set MYVAR=hello
    

    Then, %MYVAR% would be available to the calling batch file if it used:

    call called.bat
    

    But, it would not be using

    REM starts a new cmd.exe process
    start called.bat   
    
    REM stops and replaces current cmd.exe process with a new one
    called.bat        
    
    0 讨论(0)
提交回复
热议问题