2016-11-04 115 views
0

我有下面的代碼,它可以正常壓縮符合我的條件並刪除文件的文件,但我想要添加的是它計算文件夾大小之前和之後,並確定區別。這類作品但不是很好。想知道是否有更好的方法。請記住,此腳本通常運行3到4天,因爲它橫貫我們的文件系統,大小超過3TB。所以我希望它基本上不僅報告清理的內容,而且報告它回收的空間。在壓縮文件夾之前和之後計算文件大小

$Before = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum).Sum 

Function LogWrite 
{ 
Param ([string]$logstring) 

Add-content $Logfile -value $logstring 
} 

$PathToZipper="C:\Program Files\7-Zip" 
$UDSFilesFolder="U:\UDS\Test" 
$FileMask="*.sas7bdat" 
$TimeBound="12/31/2015" 
dir $UDSFilesFolder -Recurse -Filter $FileMask|where{$_.LastWriteTime -lt $TimeBound}|foreach{ 
$PathToFile=Split-Path $_.FullName 
$PrevSize=(Get-ChildItem $pwd.path | Measure-Object -property length -sum).Sum 
$ZipProcess=Start-Process "$PathToZipper\7z.exe" -ArgumentList " a -tzip `"$PathToFile\$($_.BaseName).zip`" `"$($_.FullName)`"" -Wait -PassThru 
if (!$ZipProcess.ExitCode) { 
    del $_.FullName 
    LogWrite $_.FullName 
} 
    $AfterSize=(Get-ChildItem $pwd.path | Measure-Object -property length -sum).Sum 
    $Diff = "{0:N2}" -f (($PrevSize-$AfterSize)/1MB) 
    LogWrite $PrevSize, $AfterSize, $Diff 
} 

$After = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum).Sum 
$Calc = "{0:N2}" -f (($Before-$After)/1MB) 

# *********************************************************************************************************** 
# Now lets cleanup LogFiles older than 30 days 
# *********************************************************************************************************** 

Get-ChildItem *.log | where {$_.LastWriteTime -le (Get-Date).AddDays(-30)} | remove-item -verbose 


$smtpServer = "mailserver" 
$smtpFrom = "from" 
$smtpTo = "to" 
$messageSubject = "Sample File Maintenance Script Task Completed (UDS)" 
$messageBody = @" 
This email is to inform you that the regularly scheduled tmp file maintenance script task completed successfully on DTWFP3. 

The script cleared up an extra $Calc MB of disk space. 

Current folder size is: $($colItems = (Get-ChildItem "U:\UDS\Test" -recurse | Measure-Object -property length -sum) 
"{0:N2}" -f ($colItems.sum/1MB) + " MB") 
"@ 


$smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody) 
+0

好的,我發現我的一個錯誤。我不應該使用$ pwd,因爲這給了我腳本執行目錄,以便解釋爲什麼它計算0.00空間節省。我解決了這個問題,但現在我想知道是否有一種方法可以在每個循環中節省空間,從而節省大量空間。 –

回答

0

否認,我能夠通過創建一個空數組,寫我的計算,以在foreach循環中的數組,然後將數組值一起解決這個問題。像魅力一樣工作