简体   繁体   English

如何使用PowerShell将多个文件压缩成一个zip文件?

[英]How to compress multiple files into one zip with PowerShell?

I want to compress multiple files into one zip. 我想将多个文件压缩成一个zip。

I am stuck with this at the moment: 我现在坚持这个:

Get-ChildItem -path C:\logs -Recurse | Where {$_.Extension -eq ".csv" -and $_.LastWriteTime -lt (Get-Date).AddDays(-7)} | write-zip -level 9 -append ($_.LastWriteTime).zip | move-item -Force -Destination {
    $dir = "C:\backup\archive"
    $null = mkdir $dir -Force
    "$dir\"
}

I get this exception 我得到了这个例外

Write-Zip : Cannot bind argument to parameter 'Path' because it is null. Write-Zip:无法将参数绑定到参数'Path',因为它为null。

This part is the problem: 这部分是问题:

write-zip -level 9 -append ($_.LastWriteTime).zip

I have never used powershell before but i have to provide a script, I can't provide ac# solution. 我之前从未使用过PowerShell,但我必须提供脚本,我无法提供ac#解决方案。

The problem is that Get-ChildItem returns instances of the System.IO.FileInfo class, which doesn't have a property named Path . 问题是Get-ChildItem返回System.IO.FileInfo类的实例,该类没有名为Path的属性。 Therefore the value cannot be automatically mapped to the Path parameter of the Write-Zip cmdlet through piping. 因此,该值不能通过管道自动映射到Write-Zip cmdlet的Path参数。

You'll have to use the ForEach-Object cmdlet to zip the files using the System.IO.FileInfo.FullName property, which contains the full path: 您必须使用ForEach-Object cmdlet使用System.IO.FileInfo.FullName属性压缩文件,该属性包含完整路径:

Get-ChildItem -Path C:\Logs | Where-Object { $_.Extension -eq ".txt" } | ForEach-Object { Write-Zip -Path $_.FullName -OutputPath "$_.zip" }

Here's a shorter version of the command using aliases and positional parameters: 这是使用别名和位置参数的命令的较短版本:

dir C:\Logs | where { $_.Extension -eq ".txt" } | foreach { Write-Zip $_.FullName "$_.zip" }

Related resources: 相关资源:

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

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