Search code examples
7zip

7z: Is it possible to exclude files conditionally?


It is possible to exclude files from zipping them with the 7zip -x switch, which allows wildcards too. So I can exclude all text files like this 7z a output.zip myfolder -x\!*.txt.

Now I want some txt files not to be excluded if they have a special name, like all text files named like this: *-KEYWORD.txt

I tried to use the exclude switch with the include switch together, like 7z a -xr\!*.txt -ir\!*KEYWORD.txt output.zip myfolder, but once the exclude switch is invoked, the include switch doesn't seem to reinclude excluded files again.

Is it possible to only include text files named like this, while excluding all other text files, inside the 7z syntax?


Solution

  • So this seems not to be possible in one command, especially not with the include and the exclude switch used both.

    The solution I use in my script now is just to make two commands, the first excludes all files ending on *.txt, then another 7z command attaches all files like *-KEYWORD.txt to the package. It's not great but it works.