########################################################
# remove log file
#* Created:
#* Version 2.0
#* Author: Shawn Shi
#* Company: PUMCH
#* Email: shicheng@live.it
#* Web: http://orca2007.spaces.live.com
#* Reqrmnts: powershell 2.0,WPK
#* Keywords:
########################################################
write-host -ForegroundColor yellow "Please input path as c:\windows\logs,input type as log,input date as 5"
$Param = New-Grid -Rows 4 -Columns 'Auto','1*' {
$TextChanged = {
$path = Get-Resource path | Select-Object -ExpandProperty Text
$type = Get-Resource type | Select-Object -ExpandProperty Text
$date = Get-Resource date | Select-Object -ExpandProperty Text
$this.Parent.Tag = "$path,$type,$date"
}
New-Label "Directory"
New-TextBox -Name path -Column 1 -On_Loaded {
Set-Resource -Name path -Value $this -Depth -1
} -On_TextChanged $TextChanged
New-Label "type" -Row 1
New-TextBox -Name type -Column 1 -Row 1 -On_Loaded {
Set-Resource -Name type -Value $this -Depth -1
} -On_TextChanged $TextChanged
New-Label "date" -Row 2
New-TextBox -Name date -Column 1 -Row 2 -On_Loaded {
Set-Resource -Name date -Value $this -Depth -1
} -On_TextChanged $TextChanged
New-Button -Name Done "Done" -Row 4 -On_Click {
#$path = $window | Get-ChildControl path
#$type = $window | Get-ChildControl type
#$date = $window | Get-ChildControl date
#if (-not $path.Text) {
# [Windows.Messagebox]::show("What is Directory?")
#}
#if (-not $type.Text) {
# [Windows.Messagebox]::show("What is type?")
#}
#if (-not $date.Text) {
# [Windows.Messagebox]::show("What is date?")
#}
$window.Close()
}
} -show
$temp=$Param.Split(",")
$fileDirectory=$temp[0]
$filetype="*."+$temp[1]
$date=$temp[2]
{
if(-not $fileDirectory )
{
write-host -ForegroundColor red "The Directory can't be null"
}
{
write-host -ForegroundColor red "The type can't be null"
}
if(!($date -ge "0"))
{
write-host -ForegroundColor red "The date can't be null"
}
else
{
$logfiles=Get-ChildItem $fileDirectory -Recurse -Force -include $filetype
foreach($logfile in $logfiles)
{
$logfilepath=Join-Path $logfile.Directoryname $logfile.Name
$LastAccessTime=Get-ItemProperty $logfilepath
$currentdate=Get-Date
$currentdate.AddDays($date)
if($LastAccessTime.LastWriteTime -lt $currentdate.AddDays(-$date))
{
Remove-Item $logfilepath -Recurse -Force
write-host -ForegroundColor red "Removing " $logfilepath
}
}
}
}
{
Write-Host "操作失败。错误原因:"$Error[0]
}
Finally
{
Remove-Variable fileDirectory
Remove-Variable filetype
Remove-Variable date
Remove-Variable Param
Remove-Variable temp
}
foreach($logfile in $logfiles)
{
$logfilepath=Join-Path $logfile.Directoryname $logfile.Name
$LastAccessTime=Get-ItemProperty $logfilepath
$currentdate=Get-Date
if($LastAccessTime.LastWriteTime -lt $currentdate.AddDays(-5))
{
Remove-Item $logfilepath -Recurse -Force
}
}