我正在尝试使用PowerShell来压缩文件。
我安装了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
的问题:
-EntryPathRoot
与Write-zip
一起使用,否则它将无法获取网络共享"Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist."
,为什么从C:\Windows\system32\
而不是\\network\share
删除文件Write-zip -append
确实将文件添加到zip文件中,但它不只是在该zip文件的根目录中添加文件,它实际上在zip的根目录中创建了整个文件夹结构,并添加了更新的过滤文件。该文件夹结构的结尾。我只想将较新的过滤文件添加到该zip文件的根目录中。请问好吗?
答案 0 :(得分: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