It occurred to me that 90% of my backup scripts just run robocopy commands, many of which do not rely on a previous command - so could I launch a batch file and have all the commands in it run simultaneously in multiple threads rather than each command having to complete before the next one can run? Seems logical and realistic based on today's multi-core processors and gigabytes of RAM.
Then I thought that actually some of my commands do rely on a predecessor completing first, so what I want is a multi-stage solution where one stage cannot begin until the last stage has completed but within each stage all the commands should be executed simultaneously.
I could find no easy way to do this with traditional batch files and so began Mutli Stage Threading (or MST) for short.
It's a fairly raw little application with no command line help what-so-ever (at least not at the time of writing) and no command line parameters. To use it is pretty simple; put the executable in a folder then in the same folder create a file named Stage1.cmd in to which you add your batch commands. Create a Stage2.cmd file and pop in some more commands. Create Stage3.cmd and so on. Run MST and all the commands in Stage1.cmd will all be created in separate threads. Stage2.cmd will not begin until every command in Stage1.cmd has completed.
I chose to use the .cmd extension so that the files remained proper batch files that could also be run outside of MST.
It does have some limitations with scope for improvement:
- Stage files must be in the same folder as MST.exe, so if you want to run multiple jobs you need to keep multiple copies of MST.exe in a number of folders. Solution: add a command line parameter that specifies the folder that contains the stage files you want to launch. DONE
- Stage files must have single, standalone commands on each line. MST does not support proper batch files that have labels, error levels and forking. Solution: have MST examine the first line of each stage file. If it is "REM NOTHREADING" then execute the whole file in a single thread and wait for it to complete. DONE
- The first word on each line of the stage files must be a command application i.e. robocopy. It can not be "call" or "start". Solution: probably same as previous solution - execute whole file.
- Stage files must be named Stage1.cmd, Stage2.cmd etc.. Solution: have MST look for a file named stages.mst. If this file exists then assume that it contains a list of scripts to execute so start at the top and work down. DONE
- If you have Stage1.cmd to StageN.cmd and you delete StageN-6.cmd then the loop is broken because MST stops as soon as the next stage file does not exist. Solution: have a command line parameter in which you can specify the minimum number of files to check for so that if Stage48.cmd does not exist MST will still check for the existence of Stage49.cmd so long as you specify on the command line to look for 49 or more stages. DONE
- There is currently no way to specify the maximum amount of threads to use. If a stage file contains 30 commands then MST will launch 30 threads. If this is too many then you need to split the commands over multiple stage files. Solution: have a command line parameter to specify the maximum number of threads to use per stage.
