Code Agnostic
Random adventures across platforms

Creating a Tar Archive From a File List in Windows

This task is relatively straightforward under *nix systems, where you can do much more robust command substitution on the command line. Here's one sample I found online:

tar cfvz test.tar.gz `cat manifest.txt`

That backtick in Unix sticks the output of the contained command into place on the command line. So suppose you had the following text in manifest.txt:

thisfile.txt
thatfile.txt


The command that would get executed would then look like this:

tar cfvz test.tar.gz thisfile.txt thatfile.txt

Unfortunately, Windows doesn't have a similar functionality. But with a little creative use of pipes and a Unix command substitute (like CygWin or MKS Toolkit), you can fake it.

Basically, this happens through the use of xargs, which lets you perform a type of substitution similar to the Unix backtick functionality. But instead of being in-line the substitution is performed serially. That is, instead of having the substitution right in place, you perform the first command, pipe it to xargs, then let xargs do the substitution.

By default, xargs does the substitution by appending whatever comes in through the pipe to the command given to xargs. So, for example:

echo file.txt | xargs ls -l

The net result of this is:

ls -l file.txt

You can also do midline subsitution with the '{}' placeholder, which requires the -i parameter to be specified. For example, this:

echo file1.txt | xargs -i copy '{}' file2.txt

Results in this:

copy file1.txt file2.txt

So let's put it together for tar! You want to send out all of the contents of your manifest and add them to the tar, right? So cat your manifest file, pipe that to xargs, and use xargs to create the actual tar command. This ends up looking like this:

cat manifest.txt | xargs tar cvf myArchive.tar

I'd be interested in any other approaches people might have for working around this limitations in the Windows shell, especially any that don't use (or make minimal use of) Unix tools. To me, the Holy Grail for this would be to be able to do this relatively easily with WinZip, to make the resulting archive more portable to less-technical Windows users.
 

0 comments so far.

Something to say?