OK, so most of you are going to click off this post pretty fast. It’s ok. I just need to get this code out there for posterity’s sake. It took me HOURS to figure some of this out so if I can save just one person, this post is worth it. It seems so easy looking back but when you’re not familiar with the syntax, it really is like trying to write Greek and not knowing the language.
Here’s the run down of what needed to happen. I had one machine with a directory of files. I needed those files distributed to 10 other machines evenly in small groups each day so they could process the files in parallel. Over a span of 30 days, I wanted to send each of the 10 computers 4 files so that each day 40 files were processed. Oh, and the files needed processed in descending name order.
Enter this script.
A few things to note here.
1 – The batch file takes two parameters. First, is the number of files you want distributed at that time. The second parameter is the directory where the files are.
2 – The big issue I had was the variable counter was not holding its value. The trick is you have to use the
setLocal EnableDelayedExpansion AND use the exclamation marks so it knows to use the current value. Otherwise, it waits until the loop is done to parse. Also, please note if you echo those values, it won’t look right but it is doing the math right.
3 – Another issue was the modulus. Make sure you use the /a to for the set command so the operation is an arithmetic operation. Otherwise it treats it as a string. Also, when you’re in a batch script, use two percent signs %% to do the mod. You’ll want to change the value in the mod to however many computers you have to send to.
4 – In the for loop, the /A-D switch tells it to NOT show directories, the /B switch tells it only to use the bare format, and the /O-N switch tells it to Order it by Name in Descending order (the minus sign).
5 – The %cd% is a standard variable that tells it to use the current directory that the script is running from.
The script does make some assumptions on the directories and such but I’m sure you can clean those up.
Download the code here if you’d like to use any of it. You’ll just rename the extension to .bat and if you want to run it, here’s the command:
code.bat 40 c:directoryoffiles
I hope that helps someone a little!
dani says
Have NO idea what you’re talking about but proud to be bff with such a smarty pants.