Powershell 7zip (7za) 排除文件类型
如何使用 powershell 和 7zip ( 7za.exe ) 对文件夹进行 ZIP 压缩,同时排除某些文件类型?
我尝试了这个:
cd "C:\path\to\folder to zip"
7za.exe a "C:\path\to\newZip.zip" -mx3 -x!*.txt -x!*.pdf
但返回:
.txt: WARNING: The system cannot find the file specified.
.pdf: WARNING: The system cannot find the file specified.
并且不压缩任何东西 - 只是创建一个空的 ZIP 文件。
我也尝试过这个:
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
但是压缩了“C:\path\to\folder to zip”文件夹中的所有内容,而不是排除任何内容。
感谢您提供的任何帮助。
-吉姆
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的第二次尝试几乎是正确的。
调用 7-zip 的命令需要包装在 for-each 块中,否则
$_.FullName
将解析为空字符串和 7-zip (在没有输入参数的情况下)自动压缩目录中的所有内容。因此将其更改为:注意
%
是 foreach-object 的别名。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:Note that
%
is an alias to foreach-object.