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

How to Zip File By Month with 7Zip

我有下面的 PowerShell 脚本,它会将所有文件压缩到一个 zip 文件中,并将当前月份作为 zip 文件名 (June 2017.7z)。

# set folder path
    $dump_path = "C:\Users\Desktop_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-Zipz.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 以删除归档文件。

DateTime 对象有一个 .addMonths 方法,您可以使用它来代替 .addDays 方法以及一个月份 属性。您可以使用 Get-Date | Get-Member

观察所有成员
# set folder path
$dump_path = "C:\Users\Desktop_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-Zipz.exe" u -mx9 -t7z -m0=lzma2 ($group.Name + ".7z") $file.FullName
  }
}

在性能方面,IMO 的分组毫无意义。 文件被存储、分组,最后被读出,每个文件都被压缩。

即时确定正确的 zip 应该没有问题。

# set folder path
$dump_path = "C:\Users\Desktop_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-Zipz.exe" u -mx9 -t7z -m0=lzma2 $Zip $_.FullName |Out-Null
    If ($LastExitCode -eq 0) { Remove-Item $_.FullName }
  }

编辑 已更改

  • 月份名称缩写加下划线
  • 将 7z 的输出重定向到 Out-Null
  • 检查$LastExitCode如果零删除文件

我能够找出我的问题。谢谢你们的帮助。如果有人需要,下面是脚本。

# set folder path
$log_path = "C:\Users\pdo\Desktop_2017\log\*.log"
$zip_path = "C:\Users\pdo\Desktop_2017\log\*.7z"
$target_path = "C:\Users\pdo\Desktop_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-Zipz.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