Windows call cmd from cmd

A windows batch file (called.bat or called.cmd) can be called from another batch file (caller.bat or caller.cmd) or interactive cmd.exe prompt in several ways:

  1. direct call: called.bat
  2. using call command: call called.bat
  3. using cmd command: cmd /c called.bat
  4. using start command: start called.bat

I’m quite in trouble to differentiate their intended usage based on their help text: when to use which one? e.g. why I might use ‘call’ command instead of direct call. What’s different?

I’m interested on some summary report that analyze all 4 possibilities (and others if any missing) from various point of views: recommended use cases for which they are designed to fit, process spawning, execution context, environment, return code processing.

Note: I’m using Windows XP SP3.

asked Apr 9, 2010 at 13:35

dim's user avatar

  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.

answered Apr 9, 2010 at 14:11

Kyle Alons's user avatar

Kyle AlonsKyle Alons

6,9752 gold badges33 silver badges28 bronze badges

2

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        

answered Mar 9, 2017 at 16:21

ken's user avatar

kenken

711 silver badge3 bronze badges

A windows batch file (called.bat or called.cmd) can be called from another batch file (caller.bat or caller.cmd) or interactive cmd.exe prompt in several ways:

  1. direct call: called.bat
  2. using call command: call called.bat
  3. using cmd command: cmd /c called.bat
  4. using start command: start called.bat

I’m quite in trouble to differentiate their intended usage based on their help text: when to use which one? e.g. why I might use ‘call’ command instead of direct call. What’s different?

I’m interested on some summary report that analyze all 4 possibilities (and others if any missing) from various point of views: recommended use cases for which they are designed to fit, process spawning, execution context, environment, return code processing.

Note: I’m using Windows XP SP3.

asked Apr 9, 2010 at 13:35

dim's user avatar

  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.

answered Apr 9, 2010 at 14:11

Kyle Alons's user avatar

Kyle AlonsKyle Alons

6,9752 gold badges33 silver badges28 bronze badges

2

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        

answered Mar 9, 2017 at 16:21

ken's user avatar

kenken

711 silver badge3 bronze badges


Asked by Brooklyn Henry on Nov 30, 2021

Shell — An example of programming



The environment Variable %CmdCmdLine% will expand into the original command line passed to CMD.EXE Echo %CmdCmdLine% | findstr /c:» /c » >nul && Echo Started with a double click. To run a PowerShell script from the CMD shell:

In this manner, how to run CMD.EXE commands in PowerShell?
Running CMD commands in PowerShell. PowerShell by default does not support the native cmd.exe command such as „dir“. Instead, it uses historic aliases called “dir” to point you to the closest PowerShell cmdlet: This explains why “dir” in PowerShell won’t support the switches and arguments it used to in cmd.exe and batch files, like “cmd.exe /w”.
Likewise, how to run CMD / C command from MS DOS? We will click to the Start Menu and write down the complete command. This will also show the command with the MS-DOS icon like below. This will work like previous examples. Alternatively, we can run cmd /c command from a regular command line or MS-DOS. This will open a new MS-DOS shell and run given command.
Next, how do I run a file from command prompt?
Hit ↵ Enter or ⏎ Return on your keyboard. This will navigate you into the selected file path in Command Prompt. Type start [filename.exe] into Command Prompt. This command will allow you to run a program from the selected file path. Replace [filename.exe] with your program’s name.
Accordingly, are there any internal commands in the CMD shell?
CMD Internal- Commands that are Internal to the CMD shell. Q156276- Cmd does not support UNC names as the current directory. Powershell: You can run the CMD shell under Powershell, Exit will return you to the PS prompt. Equivalent bash command (Linux): bash — run the bash shell (also csh, ksh, sh).


I need to run two commands which never terminate. What I normally do is to

  • start a cmd terminal and type in the first command
  • start another cmd terminal and type in the second command

I then have my two processes running in parallel.

I now would like to automate this by having one «startup» file which would lauch the two terminals above. It can be cmd or PowerShell based.

Note 1: I tried to use cmd with /k or /c but this does not spawn a new terminal. Trying something like cmd /c cmd ended up with Internal Error output in the shell.

Note 2: PowerShell has Background Jobs. The problem is that I want to have two separate shells I can monitor the output on (and eventually close the running process with Ctrl-C.

Is there a way to achieve this in one file?

asked Oct 12, 2017 at 12:03

WoJ's user avatar

Create a batch file:

start cmd.exe /c <first command>
start cmd.exe /c <second command>

Run the batch file and it will open the two cmd windows and the batch file will exit.

answered Oct 12, 2017 at 12:27

Appleoddity's user avatar

AppleoddityAppleoddity

11.6k2 gold badges24 silver badges41 bronze badges

4

I believe ‘start cmd /c dir’ is what you’re looking for.
edit: Well, /k for short example like dir, but ‘start’ is the magic word you need.

answered Oct 12, 2017 at 12:20

Ren's user avatar

RenRen

1572 silver badges14 bronze badges

You must log in to answer this question.

Not the answer you’re looking for? Browse other questions tagged

.

%comspec% /k «C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat»

copype amd64 c:\winpe_x64 & cd «..\..\Assessment and Deployment Kit\Windows Preinstallation Environment» #Мне нужно запустить команды одна за другой в консоли Deployment and Imaging Tools Environment
как это сделать?


  • Вопрос задан

  • 2650 просмотров

Пригласить эксперта

Батник и так выполняется внутри cmd. Обычно дополнительно cmd запускать не надо.
Просто напишите нужные команды в батнике и запускайте в cmd или в проводнике (тогда cmd запустится автоматически и выполнит батник).

Если нужно вызвать батник из батника, нужно использовать команду call:
call batnik.bat


  • Показать ещё
    Загружается…

09 окт. 2023, в 11:14

1500 руб./за проект

09 окт. 2023, в 11:12

2500 руб./за проект

09 окт. 2023, в 11:10

300 руб./за проект

Минуточку внимания

  • Windows boot manager переустановка виндовс
  • Windows boot manager пропал из биоса
  • Windows boot manager ошибка 0xc0000098
  • Windows boot manager перевод на русский язык
  • Windows boot manager не открывается