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:
FOR /L %%Y IN (1996,1,2008) DO C:\Users\Laur\Scripts\load.bat %%Y
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:
- Make load.bat print something
- 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:
C:\Users\Laur\Scripts\load.bat %%Y >> C:\Users\Laur\Scripts\logs\all.log
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:
FOR /L %%Y IN (1996,1,2008) DO ( ECHO * %%Y ECHO * Command: C:\Users\Laur\Scripts\load.bat %%Y C:\Users\Laur\Scripts\load.bat %%Y >> logs\all.log )
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.