I'm trying to create a 7-Zip archive that would compress a certain folder (only specified one!) while excluding a certain file extension. I've come up with this:

"PATH-TO-7Z.EXE" a archive.7z "C:\tools\" -t7z -mx=9 -mhe=on -mtc=on -sccUTF-8 -scsUTF-8 -ssc- -ssw -y -slp -r -x!*.avi

What this does, however, is SCANNING whole c drive looking for tools (C:\tools\ is given as a target in the example above) as keyword and adding anything that it finds to my archive.

Is there a way to avoid this? Meaning to ONLY archive C:\tools\ while excluding specified extensions.

I think it's something about -r option, but I'm not sure what.


7-Zip searches based on the current directory. This bit of information isn't explicitly documented, but it's implied in the help for the Add command:

cd /D c:\dir1\
7z a c:\archive3.zip dir2\dir3\ 

The filenames in archive c:\archive3.zip will contain dir2\dir3\ prefix, but they will not contain c:\dir1\ prefix.

You'll need to change your current directory before starting 7-zip, so your command would look like:

pushd "C:\Tools" & C:\Path\To\7z.exe a -r -x!*.avi archive.7z * & popd

You can also split that string onto 3 lines if you'd like.

Also of note, you've got a lot of unnecessary options in your command line:

  • -ssc-: Default on a Windows system
  • -t7z: Only needed when the archive type can't be determined from the extension (e.g. -tzip must be specified if you want to create an .xpi file)
  • -mhe=on: You'll need to double check, but I don't think that header encryption does anything on an unencrypted archive.
  • -scsUTF-8: Default Setting
  • -sccUTF-8: Only required if you want to read the console output (or redirect to file) and you actually need UTF-8 for that particular purpose
  • -y: Doesn't work with the Add command at all (-e or -x only).
  • -ssw: If you think you need this, you should look at operating on a VSS snapshot so you can ensure that you're getting a consistent copy of the file that's being read. If this is being used with a particular application whose behavior you've tested, then you can ignore this point
  • -slp: Make sure you've read the help and understand both the requirements and effects of this option, and that you've tested it to ensure that it doesn't have an adverse effect on your environment.
  • Thank you so very much! Works like a charm! One thing though, it creates an archive in the folder which I want archived (in my example it would be C:\tools). Is it possible to tweak it a bit so it would create the resulting archive either where the source bat file is (the string is run from a simple .bat) or at a custom location? But in any case, thank you so so much again!! – Amore Jul 5 '12 at 8:37
  • For a custom archive location, all you need to do is specify the full path (e.g., C:\Where\Backups\Go\Archive.7z) instead of just Archive.7z. To put the archive in the same location as the script, %~dp0\Archive.7z should work. – afrazier Jul 5 '12 at 12:48
  • afrazier, thank you SO very much! It works like a charm and you're an absolute life-saviour! I am very, very much obliged!!! – Amore Jul 24 '12 at 13:06

Yep, the -r option doesn't necessarily do what you expect. This is now mentioned in the FAQ: Why doesn't -r switch work as expected?:

Why doesn't -r switch work as expected?

In most cases you don't need -r switch. 7-Zip can compress subfolders even without -r switch.

Example 1:

7z.exe a c:\a.7z "C:\Program Files" compresses "C:\Program Files" completely, including all subfolders.

Example 2:

7z.exe a -r c:\a.7z "C:\Program Files" searches and compresses "Program Files" in all subfolders of C:\ (for example, in "C:\WINDOWS").

If you need to compress only files with some extension, you can use -r switch: 7z a -r c:\a.zip c:\dir*.txt compresses all *.txt files from folder c:\dir\ and all it's subfolders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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