2016-09-22 246 views
1

我想用Powershell自動執行: 1.壓縮7天以前的日誌文件(.xml和.dat擴展名), 2.將這些壓縮的壓縮文件其他地方和 3.然後從源中刪除原始日誌文件。Powershell壓縮文件夾和文件,然後刪除舊文件

我使用以下Powershell腳本,我從各種資源拼湊在一起。

function New-Zip 
    { 
    param([string]$zipfilename) 
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    (dir $zipfilename).IsReadOnly = $false 
    } 

function Add-Zip 
    { 
    param([string]$zipfilename) 

    if(-not (test-path($zipfilename))) 
    { 
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    (dir $zipfilename).IsReadOnly = $false 
    } 

    $shellApplication = new-object -com shell.application 
    $zipPackage = $shellApplication.NameSpace($zipfilename) 

    foreach($file in $input) 
    { 
     $zipPackage.CopyHere($file.FullName) 
     Start-sleep -milliseconds 500 
    } 
    } 

$targetFolder = 'C:\source' 
$destinationFolder = 'D:\destination\' 
$now = Get-Date 
$days = 7 
$lastWrite = $now.AddDays(-$days) 

Get-ChildItem $targetFolder -Recurse | Where-Object { $_ -is [System.IO.FileInfo] } | ForEach-Object { 
If ($_.LastWriteTime -lt $lastWrite) 
{ 
    $_ | New-Zip $($destinationFolder + $_.BaseName + ".zip") 
    $_ | Add-Zip $($destinationFolder + $_.BaseName + ".zip") 
} 
} 

Get-ChildItem $targetFolder -Recurse -Include "*.dat", "*.xml" | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$days))} | Remove-Item -Force 

這個腳本確實工作得相當好,因爲它歸檔文件,並將其複製在目標文件夾。

如果我有℃的結構:\源\ bigfolder \ logfile.dat,所產生的壓縮文件不會得到的文件夾結構,因爲我想:

logfile.zip> bigfolder>日誌.DAT

相反,它只是得到:logfile.zip> logfile.dat

可以搞清楚了這一點別人的幫助?

爲了更好地調整它,我想如果可能的話建立一些邏輯,所以文件只有在滿足特定條件時才被壓縮。

,我壓縮原始日誌文件有一個命名慣例如下:

Folders: 
    emstg#12_list\randomstring.xml 

Individual log files: 
    emstg#12_query_data.xml 
    emstg#12_events_cache.dat etc... 

正如你可以看到這些文件的開始一樣的是emstg#號。

如何在上面的腳本中實現「名稱檢測」機制?

感謝

回答

0

您可以通過使用[System.IO.Compression] 壓縮文件夾我寫了這基於你的腳本。 我的想法是將需要壓縮的文件的整個文件夾結構複製到臨時文件夾中,然後壓縮該臨時文件夾。 對於名稱檢測,你只需要另一個where-object(修改代碼,只要你想)

function Zip 
    { 
    param(
     [string]$source, 
     [string]$des 
    ) 
    add-type -AssemblyName System.IO.Compression.FileSystem 
    [System.IO.Compression.ZipFile]::CreateFromDirectory($source,$des,'Optimal',$true) 
    Start-sleep -s 1 
} 


$targetFolder = "C:\source" 
$destinationFolder = "C:\destination" 
$temp = "C:\temp" 
$now = Get-Date 
$days = 7 
$lastWrite = $now.AddDays(-$days) 
$i = 1 

Get-ChildItem $targetFolder -Recurse | Where-Object { $_ -is [System.IO.FileInfo] } | Where-Object {$_ -like "*.*"} | ForEach-Object { 
    If ($_.LastWriteTime -lt $lastWrite) { 
     $halfDir = $_.DirectoryName.Trim($targetFolder) 
     $s = $temp + "\" + $i + "\" + $halfDir 
     $d = $destinationFolder + "\" + $_.BaseName + ".zip" 
     Copy-Item $_.DirectoryName -Destination $s 
     Copy-Item $_.FullName -Destination $s 
     Zip -source $s -des $d 
     $i++ 
    } 
} 
+0

THX,一些錯誤 如果我有一個文件夾和多個文件相同的命名約定在文件名的開始,如** D:\ source \ emstg#12 \ string.xml **(文件夾&文件),然後單個文件:** D:\ source \ emstg#12_data.xml **或** D:\ source \ emstg# 12_events.dat **,由於類似的名稱導致錯誤:**使用「4」參數調用「CreateFromDirectory」的異常:「文件'D:\ destination \ esmstg#12.zip'已經存在。」* *臨時文件也不會被刪除。 修改後的代碼是:..... ..... | Where-Object {$ _ -like「esmstg#12 *」} | ForEach-Object {' ZIP應該保持相同的文件夾/文件結構 – valdroni

相關問題