Powershell 7zip (7za) 排除文件类型

发布于 2025-01-08 16:31:42 字数 682 浏览 2 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-01-15 16:31:42

你的第二次尝试几乎是正确的。

调用 7-zip 的命令需要包装在 for-each 块中,否则 $_.FullName 将解析为空字符串和 7-zip (在没有输入参数的情况下)自动压缩目录中的所有内容。因此将其更改为:

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

注意 % 是 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文