简体   繁体   English

使用Powershell压缩文件

[英]zip files using powershell

This is what I am trying to do with powershell for zipping files. 这就是我试图使用Powershell压缩文件的方式。

  1. Sort out files older than xxx days 整理超过xxx天的文件
  2. Add those files in a zip file. 将这些文件添加到zip文件中。
  3. Delete the source files. 删除源文件。

I have Powershell_Community_Extensions installed so I use Write-zip to do the job. 我已经安装了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

Issues: 问题:

  1. I have to use -EntryPathRoot with Write-zip , otherwise it wont pick up network share 我必须将-EntryPathRootWrite-zip一起使用,否则它将无法获取网络共享
  2. Remove-item also wont pickup files from network share, it says "Remove-Item : Cannot find path 'C:\\Windows\\system32\\feb03.txt' because it does not exist." Remove-item也不会从网络共享中提取文件,它说"Remove-Item : Cannot find path 'C:\\Windows\\system32\\feb03.txt' because it does not exist." , why it deleting file from C:\\Windows\\system32\\ instead of \\\\network\\share ,为什么它从C:\\Windows\\system32\\而不是\\\\network\\share删除文件
  3. Write-zip -append did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. Write-zip -append确实将文件添加到zip文件中,但它不仅在zip文件的根目录中添加了文件,它实际上在zip的根目录中创建了整个文件夹结构,并在此末尾添加了更新的过滤文件文件夹结构。 I just want to add the newer filtered files into root of that zip file. 我只想将更新的过滤文件添加到该zip文件的根目录中。

Any idea please? 有什么想法吗?

Utilizing the v5 *Archive cmdlets: 使用v5 *归档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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM