2017-06-22 116 views
1

我有下面的PowerShell腳本,它會將當前月份的所有文件壓縮成一個zip文件作爲zip文件名(June 2017.7z)。如何使用7Zip按月壓縮文件

# 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刪除歸檔文件。

+0

你的腳本看起來在過去的30天,是你想要什麼?或者你想按月份(Jun,Jul等)? – TheIncorrigible1

+0

TheIncorrigible1,謝謝你指出。我需要歸檔所有超過30天的文件,並在歸檔後刪除文件。這個腳本文件將每月執行一次。 – Mixer

回答

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 
0

DateTime對象有一個.addMonths方法可以用來代替.addDays方法以及Month屬性。您可以通過使用Get-Date | Get-Member

# 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 
    } 
} 
+0

這會縮短代碼。謝謝! – Mixer

+0

如何將分別在同一個月創建的文件歸檔? – Mixer

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 } 
    } 

編輯改變

  • 月份名稱即可abriviation和下劃線
  • 的7z格式輸出重定向到Out-空
  • 檢查$LastExitCode如果零刪除文件
+1

OP提到他需要一個月以上的東西,所以你不妨做'GCI $ dump_path |?{$ _。LastWriteTime.Month -le [datetime] :: today.addmonths(-1).Month -and!$ _.PSIsContainer}'管道到你的'ForEach'。此外,爲了保持一致性,可以引用'LastWriteTime'或'CreationTime',但不要混合和匹配。不是在指責你,OP是做到了,但是如果你正在改進代碼,那麼你可能還會付出更多努力。 – TheMadTechnician

+0

好吧,如果我考慮一下,不同的日期可能有意義。歸檔文件在上個月沒有改變是一回事,但歸檔時沒有改變創建時間(撇開一些應用程序重寫文件而不保留創建日期)。 – LotPings

+0

@LotPings,您的代碼沒有將超過30天的文件壓縮到不同的zip文件中,並根據創建/修改文件的日期命名帶有月份和年份的zip文件。 – Mixer

相關問題