【问题标题】:Powershell: Compress filesPowershell:压缩文件
【发布时间】:2014-06-12 10:11:13
【问题描述】:

我需要根据编写的月份和年份来压缩文件。

# group files based on extension month/year
$groups = Get-ChildItem C:\Users\mypath -filter "*psv.*" | Group-Object {"{0:MMMM} {0:yyyy}" -f $_.LastWriteTime }

# compress groups
ForEach ($group in $groups) {
    & "C:\Program Files\7-Zip\7z.exe" u ($group.FullName + ".7z") $group.FullName
}

上面的脚本不起作用,但是,当我单独运行以下行时

Get-ChildItem C:\Users\mypath -filter "*psv.*" | Group-Object {"{0:MMMM} {0:yyyy}" -f $_.LastWriteTime }

我得到以下结果,这很好。

但是,它不会压缩文件组。

【问题讨论】:

    标签: powershell powershell-2.0 windows-server-2008-r2 powershell-3.0 powershell-1.0


    【解决方案1】:

    试试这个:

    $groups = Get-ChildItem C:\Users\mypath *psv.* | Group {"{0:MMMM}-{0:yyyy}" -f $_.LastWriteTime}
    foreach ($group in $groups) { 
        foreach ($file in $group.Group.FullName) { 
             Write-Host "Processing " + $group.Name + " " + $file 
             & "C:\Program Files\7-Zip\7z.exe" u ($group.Name + ".7z") $file
        } 
    }
    

    GroupInfo 输出的 GroupInfo 对象的 Group 属性包含该分组中的所有文件。您必须枚举这些文件。

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多