简体   繁体   中英

Powershell 7zip (7za) Exclude File types

How can I use powershell and 7zip ( 7za.exe ) to ZIP a folder while excluding certain file types?

I tried this:

cd "C:\path\to\folder to zip"
7za.exe a "C:\path\to\newZip.zip" -mx3 -x!*.txt -x!*.pdf

But that returns:

.txt:  WARNING: The system cannot find the file specified.

.pdf:  WARNING: The system cannot find the file specified.

and doesn't ZIP anything- just creates an empty ZIP file.

I have also tried this:

cd "C:\path\to\folder to zip"
Get-ChildItem "C:\path\to\folder to zip" -Recurse -Exclude *.txt, *.pdf | 7za.exe a -mx3 "C:\path\to\newZip.zip" $_.FullName

But that ZIPs everything in the "C:\\path\\to\\folder to zip" folder instead of excluding anything..

Thank you for any help you can provide.

-Jim

Your second attempt is almost correct.

Your command to call 7-zip need to be wrapped in a for-each block, otherwise the $_.FullName is resolved to an empty string and 7-zip (in the absence of the input parameters) automatically zips everything in the directory. So change it to this:

Get-ChildItem "C:\path\to\folder to zip" -Recurse -Exclude *.txt, *.pdf | %{7za.exe a -mx3 "C:\path\to\newZip.zip" $_.FullName}

Note that % is an alias to foreach-object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM