How to use 7-zip to add a folder to an archive, including only files with certain extensions

7-zip

I know that the 7-zip command to add a folder to an archive is, e.g.:

"c:\program files\7-zip\7z" a testArchive.zip c:\myFolder

And the command to add only files of a certain extension to an archive is, e.g.:

"c:\program files\7-zip\7z" a testArchive.zip c:\myFolder\*.jpg -i!c:\myFolder\*.pdf

How do I add a folder to an archive containing only files of a certain extension? I tried:

"c:\program files\7-zip\7z" a testArchive.zip c:\myFolder\ -i!c:\myFolder\*.jpg -i!c:\myFolder\*.pdf

but that just added all the files in the folder, not just the .jpg and .pdf. What's more, it also added subfolders, which I didn't want.

Is there a way to use the -x switch so that I can say, "add all files except those that are not these extensions, and don't add any subfolders"?

Best Answer

Your command told 7z to add the data in myFolder, then also to add stuff in myFolder that was a jpg, then that which is a pdf. You can just use:

"c:\program files\7-zip\7z" a testArchive.zip -i!c:\myFolder\*.jpg -i!c:\myFolder\*.pdf

and that will pick up the correct files through the inclusion filters.