【问题标题】:zip files using powershell使用 powershell 压缩文件
【发布时间】:2018-05-20 16:48:01
【问题描述】:

这就是我试图用 powershell 来压缩文件。

  1. 整理出超过 xxx 天的文件
  2. 将这些文件添加到 zip 文件中。
  3. 删除源文件。

我安装了 Powershell_Community_Extensions,所以我使用 Write-zip 来完成这项工作。

$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}

$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet

Remove-Item $files -Force

问题:

  1. 我必须使用-EntryPathRootWrite-zip,否则它不会获取网络共享
  2. Remove-item 也不会从网络共享中提取文件,它说"Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.",为什么它从C:\Windows\system32\ 删除文件而不是\\network\share
  3. Write-zip -append 确实将文件添加到 zip 文件中,但它不只是在该 zip 文件的根目录中添加文件,它实际上在 zip 的根目录中创建了整个文件夹结构,并在该文件夹结构的末尾添加了更新的过滤文件.我只想将新过滤的文件添加到该 zip 文件的根目录中。

有什么想法吗?

【问题讨论】:

  • 您使用的是哪个版本的 PowerShell?如果是 v5,则有内置的*Archive cmdlet
  • 嗯....不知道,我在 v5 上。我现在就试试。

标签: powershell zip


【解决方案1】:

利用 v5 *Archive cmdlet:

$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update

$Files | Remove-Item -Force

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2023-03-22
    • 2018-09-30
    • 2017-11-03
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多