Quick On-The-Fly Batch Processing Using `for` Loop in `cmd` (Windows)

Peril of bat attempting to drink coffee upside down

Bats spilling coffee because they hang out upside down
Because .bat file (amirite?)

I thought I’d throw this up real quick, because I am constantly using for loops in bash to do batch processing, but I wasn’t as familiar about how to do them in Windows. Powershell is undoubtedly more powerful, but also incredibly verbose, so if you want to do something quick and simple like this, cmd isn’t too bad, as long as your requirements aren’t overly complicated.

The vcredists-all package in the chocolatey repo appears to be broken, so I set up this super quick and dirty for-loop in cmd for batch-running all the vc runtime installers. I was bootstrapping a new machine that was light on runtimes since it’s … well … new.

Go to the official Microsoft site that has all the VC Redistributable packages listed – it’ll tell you which ones are still compatible with current releases of Windows, and which ones are deprecated. Thankfully, the download links are super easy to find. Download them all by clicking on them one by one (I know, I know …) As of writing there was 8 of them between x86 and x86_64.

You may or may want to rename them, since you’ll get a lot of vc_redist (2).exe etc.-named files. These either have to renamed manually in the explorer or using ren command to e.g. vc_redist_2.exe and so-on. Since we’re about to process them with a loop, and then (presumably) delete them afterwards, this renaming step might be unnecessary – you make the call. You might at the very least need to make sure there’s no space in each one of the file names, but the point is that each one is unique. If you have Save As set in your browser, make each name unique them during download, and that they’re all in the same folder.

.bat so silly


Then use dir to list just the vc_redist*.exe files. A quick primer on using dir in scripts, the /B flag is “bare listing”, which is removes any extra information, and lists one file per line (like ls -1). So, from there you can do:

BAT (Batchfile)
C:\User\Downloads> for %i in (vc*.exe) do cmd /c %i

And it’ll open each one individually and walk you through the prompts. Don’t worry about capitalization errors, NTFS is almost always case-insensitive.

Now that I write this, I realize it’d probably been faster to have clicked on each one than to rename half of them to get this to work 🤣 But having a handle on for loops in cmd – or all command interpreters – is a good tool to shovel into your stack heap of potential workflows.

Another thing I realize I missed is that if you run one of these files from the command line with the /? flag is a menu with options you can run them with that could prevent them from creating any dialog. This would probably be the right thing to do in this case, and I apologize for not detailing it here. Next time I have the unfortunate occasion of using a Windows machine, I’ll be sure to figure out what that is and describe it accordingly.


Leave a Reply

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