• Skip to content
  • Skip to primary sidebar

Amy J. Bennett

Extraordinary Faith for Everyday Life

  • Home
  • About
  • Archives
  • Feathers Podcast
  • Entangled eBook
  • Disclosure

Windows Batch Script – Copy Directory of Files to Multiple Computers

September 10, 2011 by Amy

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.

code

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! 

Filed Under: what i did today Tagged With: batch, command, dos, files, mod, script, windows

Reader Interactions

Comments

  1. dani says

    September 11, 2011 at 7:03 pm

    Have NO idea what you’re talking about but proud to be bff with such a smarty pants.

Primary Sidebar

Feed Twitter Facebook Email Feed Feed

Welcome


Hey! I'm so glad you're here. I'm Amy, working mom of 3 in the Southern suburbs. I love Jesus, my family, books, chocolate and coffee. I write about faith, parenting, adoption, marriage, fashion, and design. Read more here

The Podcast

Feathers

Feathers

Feathers

  • Search
  • Categories

Popular Posts

WIWW

WIWW

WIWW

Follow Me on Instagram

Load More...Follow on Instagram

Hear My Carolina Accent

Copyright © 2025 · Infinity Pro on Genesis Framework · WordPress · Log in