ExtractArchive has the ability to Extract all files that match a pattern. For example this will extract all the JPEG files from the archive:

ExtractArchive["archive.tar.gz", $TemporaryDirectory, "*.jpg"]

How would I go about extracting all the files that don't match? I've tried:

ExtractArchive["archive.tar.gz", $TemporaryDirectory, Except["*.jpg"]]

but this doesn't work (the function pattern doesn't match).


2 Answers 2


It looks like there's a hack that I've found to work until someone comes up with a something better.

Looking at the code for ExtractArchive[], this function has a pattern that takes a list of destfiles. This wasn't obvious to me since the docs just specify that the third argument can be a 'pattern'. But given this, you can get the names of the files that you DO want first, and then give that as the third argument.

ExtractArchive["archive.tar.gz", $TemporaryDirectory, 
    Import["archive.tar.gz", "FileNames"], _?(StringMatchQ[#, "*.jpg"] &)

Except confuses me a bit in StringExpression, it can only be used on a Character. Maybe someone can chime in with a better answer about Except in StringExpression, but in 12.1 I can confirm this workaround works as a pattern directly to at least skip the Import:

___ ~~ "." ~~ ext___ /; (! StringMatchQ[ext, "png", IgnoreCase -> True])

Unfortunately this will not work in versions prior to 12.1 as CreateArchive and ExtractArchive were completely reworked in 12.1, and this is one of the bugs fixed. For the record, you can test this stuff out by checking if the pattern works properly in a StringMatchQ, internally ExtractArchive just passes the pattern to StringMatchQ anyway.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.