Create a 7-Zip including only specified folder and excluding certain extensions. No scanning

7-ziparchiving

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.

Best Answer

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.
Related Question