如何使用7Zip按月压缩文件

时间:2017-06-22 16:10:48

标签: powershell zip 7zip

我有以下PowerShell脚本,它会将所有文件压缩成一个zip文件,当前月份为zip文件名(2017年6月7日)。

# set folder path
    $dump_path = "C:\Users\Desktop\02_2017"

    # set min age of files
    $max_days = "-30"

    # get the current date
    $curr_date = Get-Date

    # determine how far back we go based on current date
    $zip_date = $curr_date.AddDays($max_days)

    # filter files
    $files = Get-ChildItem $dump_path | Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false) }

    $groups = Get-ChildItem $dump_path | 
        Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false) } | 
        group {"{0:MMMM} {0:yyyy}" -f $_.CreationTime}

    ForEach ($group in $groups) {
        ForEach($file in $group.Group){
            & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 ($group.Name + ".7z") $file.FullName
            Remove-Item $file.FullName -exclude *.ps1
        }
    }

如何修改脚本以便将每个月分别创建的所有文件压缩?例如,aaa1,aaa2,aaa3将被压缩到aug_2016.7z,bbb1,bbb2,bbb3将被压缩到feb_2017.7z,依此类推,然后删除存档文件。

8/29/2016  11:09 PM          88583 aaa1.log
8/30/2016   6:06 AM          88590 aaa2.log
8/30/2016   7:07 AM          88586 aaa3 .log
 2/1/2017   6:03 AM         179412 bbb1.log
 2/1/2017   7:03 AM         179285 bbb2.log
 2/1/2017   8:03 AM         179418 bbb3.log
 5/3/2017   6:31 PM          95764 ccc1.log
 5/3/2017   8:33 PM          95605 ccc2.log
 5/3/2017  10:34 PM          95391 ccc3.log

感谢您的帮助。

编辑: 添加了Remove-Item $ file.FullName -exclude * .ps1以删除存档文件。

3 个答案:

答案 0 :(得分:3)

性能明智,IMO的分组没有任何意义。 文件存储,分组,最后读出,每一个都压缩。

动态确定正确的拉链应该没问题。

# set folder path
$dump_path = "C:\Users\Desktop\02_2017"
# determine how far back we go based on current date
$zip_date = (Get-Date).AddMonths(0).Month

# filter files
Get-ChildItem $dump_path |
  Where-Object {(($_.LastWriteTime).Month -le $zip_date) -and ($_.psIsContainer -eq $false)}|
  ForEach {
    $Zip = "{0:MMM}_{0:yyyy}.7z" -f $_.CreationTime
    & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 $Zip $_.FullName |Out-Null
    If ($LastExitCode -eq 0) { Remove-Item $_.FullName }
  }

修改已更改

  • 到来的月份名称和下划线
  • 将7z的输出重定向到Out-Null
  • 检查$LastExitCode是否删除文件

答案 1 :(得分:0)

DateTime对象具有.addMonths方法,您可以使用.addDays方法而不是Get-Date | Get-Member方法以及Month属性。您可以使用# set folder path $dump_path = "C:\Users\Desktop\02_2017" # determine how far back we go based on current date $zip_date = (Get-Date).AddMonths(-1).Month # filter files $files = Get-ChildItem $dump_path | Where-Object { (($_.LastWriteTime).Month -eq $zip_date) -and ($_.psIsContainer -eq $false) } $groups = Get-ChildItem $dump_path | Where-Object { (($_.LastWriteTime).Month -eq $zip_date) -and ($_.psIsContainer -eq $false) } | group {"{0:MMMM} {0:yyyy}" -f $_.CreationTime} ForEach ($group in $groups) { ForEach($file in $group.Group) { & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 ($group.Name + ".7z") $file.FullName } }

观察所有成员
{{1}}

答案 2 :(得分:0)

我能够弄明白我的问题。谢谢你的帮助。如果有人需要,下面是脚本。

# set folder path
$log_path = "C:\Users\pdo\Desktop\02_2017\log\*.log"
$zip_path = "C:\Users\pdo\Desktop\02_2017\log\*.7z"
$target_path = "C:\Users\pdo\Desktop\02_2017\log\"

# set min age of files
$max_days = "-30"
$delete_max_days = "-365"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$zip_date = $curr_date.AddDays($max_days)
$delete_zip_date = $curr_date.AddDays($delete_max_days)

#$zip_date = (Get-Date).AddMonths(0).Month

# filter files
Get-ChildItem $log_path | Where-Object { ($_.LastWriteTime -lt $zip_date) -and ($_.psIsContainer -eq $false)}|

  ForEach {
    $Zip = $target_path + "{0:MMM}_{0:yyyy}.7z" -f $_.LastWriteTime
    & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2  $Zip $_.FullName |Out-Null
    If ($LastExitCode -eq 0) { Remove-Item $_.FullName }
  }

$deletefile = Get-ChildItem $zip_path | Where-Object { $_.LastWriteTime -lt $delete_zip_date } | Remove-Item