BAT Files: Execute Multiple Commands in a Loop

One of the tasks the other day was to load a large DB organised by year. The most efficient way was to loop through a sequence of years and execute the loader for each one.

The first iteration I ended with was:

This means: For years from 1996 to 1998, with an increment of one, execute my load.bat script.

Nice, but I wanted to be able to echo some information to the console. Now, there are two ways to do this:

  1. Make load.bat print something
  2. Add more commands (like an ECHO ) into the FOR loop

First option is not really a valid one, because I wanted all output from load.bat to be logged in a file (redirected) like so:

So, I’m left with the other alternative: add more commands to the FOR loop. This StackOverflow question has the bits, which helped me to evolve to this command:

You can add more commands; just make sure that there’s only one per line for clarity and the closing bracket is immediately after the last line. Apparently the interpreter is very fussy if you don’t respect the guidelines.

A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top