5

Can someone please explain what's the difference between

call someBatchFile.bat ...

And

cmd /C someBatchFile.bat ...

They both were suggested to me as solutions to this problem, but I don't understand why they both work, and more importantly, if there are any significant differences I must be aware of.

Community
  • 1
  • 1
carlossierra
  • 4,479
  • 4
  • 19
  • 30
  • 2
    Second statement starts a new process, any changes that someBatchFile.bat makes to environment variables won't affect and won't be visible to your current batch file. Could be good, could be bad. – Hans Passant Aug 20 '16 at 15:40
  • 1
    See [this answer](http://stackoverflow.com/questions/36095847/why-is-there-no-need-for-call-to-return-from-called-batch-script-which-is-invo/36096956#36096956). – Aacini Aug 20 '16 at 17:17
  • @Aacini that was the answer I was looking for. Since the linked question is different, I think it would be beneficial if you answered it here as well. Will you please do that so that I can mark it as answered? – carlossierra Aug 20 '16 at 17:48

1 Answers1

11

The difference is that call execute the other Batch file in the same context of the caller program, so they share the same environment variables and other status (like echo ON/OFF or delayed expansion), whereas cmd /C execute the other Batch file in an entirely separated context, so any change made in the called Batch file is not reflected in the original file.

Just as a personal note, I used to name internal subroutine the Batch file invoked via call, and external subroutine the one invoked via cmd /C (and overlay the Batch file directly invoked without call nor cmd /C, that inherits the behavior and context of the caller Batch file).

Aacini
  • 65,180
  • 12
  • 72
  • 108