【问题标题】:How to use Powershell Compress-Archive to compress only pdfs in a folder?如何使用 Powershell Compress-Archive 仅压缩文件夹中的 pdf?
【发布时间】:2021-10-23 23:47:20
【问题描述】:

我正在研究创建多个 pdf 并将它们输出到目录的 Alteryx 工作流程。我需要在工作流结束时调用 Powershell 脚本,将 pdf 压缩成 zip 文件并将其保存在同一位置。

我发现了这个: PowerShell Compress-Archive By File Extension,但它需要将文件复制到另一个位置然后压缩它们。有没有办法可以压缩某个文件夹位置中的所有 pdf 文件并将 zip 文件输出到同一位置?由于 Alteryx 的限制,如果输出位于其他位置,我可能无法使用它。

我当前的 Powershell 脚本:

Compress-Archive -LiteralPath 'D:\temp\test zipping flow\' -DestinationPath 'D:\temp\final.zip' -Force

这很好用,但也尝试使用其他扩展名压缩文件,但我只想要 .pdf 文件。

非常感谢任何建议。

【问题讨论】:

    标签: powershell pdf zip compression alteryx


    【解决方案1】:

    使用-Path代替-LiteralPath,这样您就可以添加通配符*

    Compress-Archive -Path 'D:\temp\test zipping flow\*.pdf' -DestinationPath 'D:\temp\final.zip' -Force
    

    当您需要使用-LiteralPath(可能是因为路径包含将使用-Path 解释的字符,如方括号)时,您可以这样做:

    Get-ChildItem -LiteralPath 'D:\temp\test zipping flow' -Filter '*.pdf' -File | 
    Compress-Archive -DestinationPath 'D:\temp\final.zip' -Force
    

    【讨论】:

    • 我尝试使用 *.pdf,但不知道我也必须更改路径参数。将 -LiteralPath 更改为 -Path 效果很好。谢谢@Theo
    猜你喜欢
    • 2021-07-11
    • 1970-01-01
    • 2019-05-02
    • 2021-12-07
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    相关资源
    最近更新 更多