Monday, March 23, 2015

Batch Script Surprise: Default Call Mode is Chain

A few weeks ago, I was experiencing a very strange phenomenon that caused my batch file to terminate unexpectedly immediately after running a CMD file (which is another extension for a batch file). After all kinds of debugging and Internet searching, I discovered something very... interesting:

Batch files, by default, exit after calling another batch file.

I was expecting my batch file to resume where it was after the other batch file finished executing, like how subroutines work in every language ever*. However, the default mode is to chain rather than call; it's as if your batch file's entire text in memory is replaced with the text of the other batch file. (Not how it works, but how I think of it.)

If you want the behavior I (and probably you also) expect, you should place the simple word "call" before the name of the batch file (leave the arguments to the batch file just the same), like so:

call copype.cmd x64 temp\

*QuickBASIC's "CHAIN" command transferred control to another program, did some memory transfer business that I still don't understand, and terminated the original program.

No comments:

Post a Comment