2012-01-11 85 views
1

\\server\logs下有一個子目錄樹,其中包含的歸檔日誌文件不遵循一致的文件命名約定。我想運行每日計劃任務,將90天或更早的非壓縮文件壓縮爲每日壓縮文件。我希望維護每個zip文件夾中的目錄樹結構和文件名。在日期中過濾Powershell中的日誌文件

在Powershell中處理此問題的最佳方法是什麼?

編輯:除非有更好的辦法,我認爲zip文件應在\\server\oldlogs創建並命名特定日期,例如。 \\server\oldlogs\20110630_logs.zip。所有在\\server\logs最後修改超過90天前的文件都應該被添加到合適的.zip文件中。

+0

在\\服務器\記錄有子目錄。這些子目錄將包含不以.zip結尾的文件。該過程應標識過去90天內尚未(創建/修改/訪問?)的所有文件,並將其放入現有目錄結構中的.zip文件中。創建存檔文件後,應該刪除那些相同的文件。準確? – billinkc 2012-01-11 18:17:59

+0

@billinkc - 現在你提到它了,在'\\ server \ oldlogs'目錄中創建zip文件似乎更簡單。我想結束一天中命名的zip文件,其中包含該日期上次修改的文件。那麼是的,一旦他們被壓縮,他們應該被刪除。我會更新這個問題。謝謝。 – 2012-01-11 18:32:40

+0

爲什麼你使用powershell標籤?你需要關於問題或功能腳本的意見嗎? – mjsr 2012-01-11 19:16:36

回答

0

這是66%的解決方案(午餐結束)。我不能讓本土拉鍊使用PowerShell的工作,但如果你有安裝PowerShell的社區擴展,應該換出拉鍊通話瞬間 How to create a zip archive with PowerShell?

# purloined from http://blogs.inetium.com/blogs/mhodnick/archive/2006/08/07/295.aspx 
# I am not smart enough to make it work. Creates an empty .zip file for me 
Function ZipIt 
{ 
    param 
    (
     [string]$path 
    , [string]$files 
    ) 

    if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

    if (-not (test-path $path)) { 
     set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    } 

    $zipFile = (new-object -com shell.application).NameSpace($path) 
    #$files | foreach {$zipfile.CopyHere($_.fullname)} 
    foreach($file in $files) 
    { 
     $zipfile.CopyHere($file) 
    } 
} 

# this script is reponsible for enumerating subfolders 
# for each file it finds, it will test the age on it 
# returns an array of all the files meeting criteria 
Function Walk-SubFolder 
{ 
    param 
    (
     [string]$RootFolder 
    ) 

    $searchPattern = "*.*" 
    $maxAge = 90 
    $now = Get-Date 

    # receiver for the files 
    [string[]] $agedOutFileList = @() 

    foreach($file in [System.IO.Directory]::GetFiles($RootFolder, $searchPattern, [System.IO.SearchOption]::AllDirectories)) 
    { 
     # test whether the file meets the age criteria 
     $age = [System.IO.File]::GetLastWriteTime($file) 
     $days = ($now - $age).Days 
     if (($now - $age).Days -ge $maxAge) 
     { 
      # this needs to be archived 
      Write-Host("$file is aged $days days") 
      $agedOutFileList = $agedOutFileList + $file 
     } 
    } 

    return $agedOutFileList 
} 

Function PurgeFiles 
{ 
    param 
    (
     $fileList 
    ) 
    foreach($file in $fileList) 
    { 
     # this should be in a try/catch block, etc, etc 
     [System.IO.File]::Delete($file) 
    } 
} 

$sourceFolder = "C:\tmp\so" 
$zipFile = "C:\tmp\so.zip" 


$oldFiles = Walk-SubFolder $sourceFolder 
ZipIt $zipFile $oldFiles 

#This is commented out as the zip process is not working as expected 
#PurgeFiles $oldFiles 

我去看看我可以工作它稍後會按預期的方式讓zip工作。

+0

感謝這很有趣。不完全正確,因爲> 90天以前的所有文件都會被添加到同一個zip文件中,而不是與上次寫入日期相對應的zip文件。我想我可以適應這個。 – 2012-01-11 20:03:58

+0

另外,ZipIt是否應該添加包含完全合格的路徑? – 2012-01-11 20:10:30

0

從.NET 3開始,System.IO.Packaging.ZipPackage可以進行壓縮。 this example中沒有子文件夾,但他們可以處理apparently

日期過濾器可以去像這樣:

$ninetyDaysAgo = (get-date).adddays(-90) 
$Files | where {$_.lastWriteTime -lt $ninetyDaysAgo}