Call a bat file from a bat file

Rather simple thing that I keep forgetting. I keep thinking this is Exec like in Sql.

To call a batch file from within another batch file you put the keyword “CALL” before the call to the batch file:

CALL MyOtherBatchFile.bat

So simply. So easy to forget when you don’t use it very often!

Concatenating Sql Scripts

To concatenate Sql Scripts into one big sql script I usually use the following rules and a batch file:

  • Always put a “GO” at the end of each script. This ensures that one script doesn’t blend with another one in the same batch.
  • Always put at least one blank line at the end and start of the script (I just do this to prevent concatenation of files resulting in the GO and something else joining together and causing an error)
  • Each file needs to be in the same encoding. See here
  • I try and order the scripts by putting a number at the start of each script. The number will help keep them in order in Explorer and also will mean they should concatenate in the correct order

I create a batch file called Concatenate.bat which has the following contents:

DEL @MyBigScript.sql
COPY /A *.sql @MyBigScript.sql

This removes the file called @MyBigScript.sql and then concatenates all .sql files into a new file called @MyBigScript.sql.

Update: The @ at the start of the filename will make the @MyBigScript.sql file show at the top of Explorer.