I seem to be having lots of these problems lately that somehow always come up, but never get solved. This time it was the problem of creating a zip file including a large source code hierarchy, but excluding certain directories along the way. More specifically, there are directories called .svn, obj or bin at various places in the hierarchy, and I don’t want to include those. Although I’ve looked around many times, I was never able to find a compression tool that would allow me to specify exclusions flexibly enough to take care of this use case — go try it with your favourite packer if you think I’m wrong. Most of them have an exclusion feature, but not a single one that I found was able to handle this case.

The solution was really simple: use some command line tools. No, not stupid Windows ones, proper GNU ones. Cygwin was, of course, already on my machine, so I simply created this little bash script:

#!/bin/bash

FIND=/usr/bin/find
GREP=/usr/bin/grep

EXCLUSION='(/(.svn|obj|bin|build)/|.suo$|.user$|.exe$|.dll$|.pdb$|~$)'

while [ "$1" != "" ]; do
    $FIND "$1" -type f | $GREP -E -v $EXCLUSION
    shift
done

This script takes file or directory names on the command lines and lists all files hierarchically from those starting points, excluding those that match particular (regular expression) patterns. Simple, isn’t it? Having this as a separate script allows me to reuse it (from the Cygwin command line, of course):

> ./listfiles.sh Demo cheatsheet.tex cheatsheet.pdf \
  "Workshop C#.pptx" | zip -9 complete.zip -@

And since I need to run that command regularly, I have created another little script that runs it. Done. On to something useful. How in the world do Windows users live without this sort of functionality? Are you one of them? Tell me! Yes… I know I should have a much closer look at PowerShell to see how it can help me with this. I have actually looked at PowerShell in some depth, but from the perspective of a programmer, not a system administrator. I promise, I’ll try to find the time to see about this, and report my findings.