7 zip - 7Zip - Command Line : Exclude folder(s) by wildcard pattern?

  • Yoopergeek

    Any idea how to exclude a wild-carded path(s) from a command-line 7zip command?

    I'm doing something this:

    7z.exe a -t7z archive.7z FolderToArchive\ -mx0

    and would like to exclude any \bin*.* or \obj*.* folders found underneath "FolderToArchive". To exclude files you can use the -x parameter. The help file gives this example for using -x:

    7z a -tzip archive.zip *.txt -x!temp.*

    That's great for excluding a file. But, again, I would like to exclude a wildcard-specified folder. Under my "FolderToArchive" there are multiple folders, under those folders there may or may not be "bin\" and "obj\" folders. I would like to not include these in the archive.

    I've tried patterns like:

    -x!bin* -x!bin*.* -x!\bin* -x!\bin*.* -x!\bin\* -x!\bin\*.*

    None seem to exclude. Is this simply a limitation of 7zip?

  • Answers
  • heavyd

    To exclude the bin and obj folders recursively you can use the command:

    7z.exe a -t7z archive.7z FolderToArchive\ -mx0 -xr!bin -xr!obj
  • user304204

    To avoid bug, use -r or -xr carefully.

    suppose you have directories like:


    and run the command:

    7z a -t7z archive.7z .\path1\path2 .\path3\path4\path5 -xr!bin

    what you got in archive.7z: .\path2\src .\path5\src

    That is, the path2 and path5 became the TOP folder in zip file. and both bin directories were excluded.

    -x only support path/filename RELATIVE TO THE FINAL PATH IN ZIP FILE. So, if you only want to exclude


    but to include all the other 'bin' directories, the command should be like this:

    7z a -t7z archive.7z .\path1\path2 .\path3\path4\path5 -x!path2\bin

    I tried to use absolute path in -x, but never succeed.

  • Related Question

    windows - 7-zip command line escaping
  • Questioner

    Is it possible to escape characters from the source file or archive when using 7-zip's command line interface. For example:

    7z a "My%Archive.zip" "My%File.txt" -mx9

    This command line fails at both creating the correct archive and choosing the correct file to zip. I would like to know how to escape the percent (%) character.

  • Related Answers
  • John T

    If the files don't actually have a percent symbol in them, you don't need to add one:

    7z a "My Archive.zip" "My File.txt" -mx9

    if you're unsure on how to type a file name, type the first few letters of the file's name then press tab for auto-completion. Keep in mind though you must type enough letters of the file's name to uniquely identify it, or you will have to press tab multiple times and cycle through other files that have the same beginning letters.

  • Area 51

    The problem stems from that fact that these commands were being issued from within a batch file. The % symbol has a special meaning in batch files. They can be escaped by doubling up the symbol (%%). Example:

    7z a "My%%Archive.zip" "My%%File.txt" -mx9

    I also tried to escape with the caret (^) but that did not work.

    The percent symbol problem has nothing to do with 7zip.

    This question was answered on SourceForge.

    SourceForge Thread