| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| param( |
| [string]$FolderPath, |
| [string]$FileAge, |
| [string]$LogFile, |
| [string[]]$ExcludePath, |
| [string[]]$IncludePath, |
| [string[]]$ExcludeFileExtension, |
| [string[]]$IncludeFileExtension, |
| [string[]]$ExcludeDate, |
| [string[]]$EmailTo, |
| [string]$EmailFrom, |
| [string]$EmailSmtpServer, |
| [string]$EmailSubject, |
| [switch]$ListOnly, |
| [switch]$VerboseLog, |
| [switch]$AutoLog, |
| [switch]$AppendLog, |
| [switch]$CreateTime, |
| [switch]$LastAccessTime, |
| [switch]$CleanFolders, |
| [switch]$NoFolder, |
| [switch]$RegExPath |
| ) |
| |
| |
| |
| |
| function ConvertFrom-DateQuery { |
| param ( |
| $Query |
| ) |
| try { |
| $CsvQuery = Convertfrom-Csv -InputObject $Query -Delimiter ',' -Header "Type","Day","Repeat" |
| $ConvertCsvSuccess = $true |
| } catch { |
| Write-Warning "Query is in incorrect format, please supply query in proper format" |
| $ConvertCsvSuccess = $false |
| } |
| if ($ConvertCsvSuccess) { |
| $Check=$HashOutput = @{} |
| foreach ($Entry in $CsvQuery) { |
| switch ($Entry.Type) { |
| 'week' { |
| |
| switch ($Entry.Day) |
| { |
| |
| 'sun' { |
| $HashOutput.DayOfWeek = 0 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'mon' { |
| $HashOutput.DayOfWeek = 1 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'tue' { |
| $HashOutput.DayOfWeek = 2 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'wed' { |
| $HashOutput.DayOfWeek = 3 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'thu' { |
| $HashOutput.DayOfWeek = 4 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'fri' { |
| $HashOutput.DayOfWeek = 5 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| 'sat' { |
| $HashOutput.DayOfWeek = 6 |
| $HashOutput.WeekRepeat = $Entry.Repeat |
| } |
| Default {$Check.WeekSuccess = $false} |
| } |
| } |
| 'month' { |
| |
| switch ($Entry.Day) |
| { |
| |
| 'first' { |
| [array]$HashOutput.DayOfMonth += 1 |
| [array]$HashOutput.MonthRepeat += $Entry.Repeat |
| } |
| 'last' { |
| [array]$HashOutput.DayOfMonth += 0 |
| [array]$HashOutput.MonthRepeat += $Entry.Repeat |
| } |
| {(1..31) -contains $_} { |
| [array]$HashOutput.DayOfMonth += $Entry.Day |
| [array]$HashOutput.MonthRepeat += $Entry.Repeat |
| } |
| Default {$Check.MonthSuccess = $false} |
| } |
| } |
| 'quarter' { |
| |
| $QuarterCount++ |
| |
| |
| switch ($Entry.Day) |
| { |
| |
| 'first' { |
| $HashOutput.DayOfQuarter = 1 |
| $HashOutput.QuarterRepeat = $Entry.Repeat |
| } |
| 'last' { |
| $HashOutput.DayOfQuarter = 0 |
| $HashOutput.QuarterRepeat = $Entry.Repeat |
| } |
| {(1..92) -contains $_} { |
| $HashOutput.DayOfQuarter = $Entry.Day |
| $HashOutput.QuarterRepeat = $Entry.Repeat |
| } |
| Default {$Check.QuarterSuccess = $false} |
| } |
| } |
| 'year' { |
| |
| switch ($Entry.Day) |
| { |
| |
| 'first' { |
| $HashOutput.DayOfYear = 1 |
| $HashOutput.DayOfYearRepeat = $Entry.Repeat |
| } |
| 'last' { |
| $HashOutput.DayOfYear = 0 |
| $HashOutput.DayOfYearRepeat = $Entry.Repeat |
| } |
| {(1..366) -contains $_} { |
| $HashOutput.DayOfYear = $Entry.Day |
| $HashOutput.DayOfYearRepeat = $Entry.Repeat |
| } |
| Default {$Check.YearSuccess = $false} |
| } |
| } |
| 'date' { |
| |
| switch ($Entry.Day) |
| { |
| {try {[DateTime]"$($Entry.Day)"} catch{}} { |
| [array]$HashOutput.DateDay += $Entry.Day |
| } |
| Default {$Check.DateSuccess = $false} |
| } |
| } |
| |
| 'daterange' { |
| |
| switch ($Entry.Day) |
| { |
| {try {[DateTime]"$($Entry.Day)"} catch{}} { |
| $HashOutput.DateRange += $Entry.Day |
| $HashOutput.DateRangeRepeat += $Entry.Repeat |
| } |
| Default {$Check.DateRangeSuccess = $false} |
| } |
| } |
| |
| 'limityear' { |
| switch ($Entry.Day) |
| { |
| {(1000..2100) -contains $_} { |
| $HashOutput.LimitYear = $Entry.Day |
| } |
| Default {$Check.LimitYearSuccess = $false} |
| } |
| } |
| Default { |
| $QueryContentCorrect = $false |
| } |
| } |
| } |
| $HashOutput |
| } |
| } |
| |
| |
| function ConvertTo-DateObject { |
| param( |
| [validaterange(0,6)] |
| $DayOfWeek, |
| [int]$WeekRepeat=1, |
| [validaterange(0,31)] |
| $DayOfMonth, |
| $MonthRepeat=1, |
| [validaterange(0,92)] |
| $DayOfQuarter, |
| [int]$QuarterRepeat=1, |
| [validaterange(0,366)] |
| $DayOfYear, |
| [int]$DayOfYearRepeat=1, |
| $DateDay, |
| $DateRange, |
| [int]$DateRangeRepeat=1, |
| [validaterange(1000,2100)] |
| [int]$LimitYear = 2010 |
| ) |
| |
| $CurrentDate = Get-Date |
| |
| if ($DayOfWeek -ne $null) { |
| $CurrentWeekDayInt = $CurrentDate.DayOfWeek.value__ |
| |
| |
| for ($j = 0; $j -lt $WeekRepeat; $j++) |
| { |
| $CheckDate = $CurrentDate.Date.AddDays(-((7*$j)+$CurrentWeekDayInt-$DayOfWeek)) |
| |
| |
| if ($CheckDate -le $CurrentDate) { |
| $CheckDate |
| } else { |
| |
| |
| $WeekRepeat++ |
| } |
| } |
| |
| |
| if ($WeekRepeat -eq -1) { |
| $j=0 |
| do { |
| $CheckDate = $CurrentDate.AddDays(-((7*$j)+$CurrentWeekDayInt-$DayOfWeek)) |
| $j++ |
| |
| |
| if ($CheckDate -le $CurrentDate) { |
| $CheckDate |
| } |
| } while ($LimitYear -le $CheckDate.Adddays(-7).Year) |
| } |
| } |
| |
| if ($DayOfMonth -ne $null) { |
| for ($MonthCnt = 0; $MonthCnt -lt $DayOfMonth.Count; $MonthCnt++) { |
| |
| for ($j = 0; $j -lt $MonthRepeat[$MonthCnt]; $j++) |
| { |
| $CheckDate = $CurrentDate.Date.AddMonths(-$j).AddDays($DayOfMonth[$MonthCnt]-$CurrentDate.Day) |
| |
| |
| |
| |
| if ($CheckDate -le $CurrentDate -and $(if ($DayOfMonth[$MonthCnt] -ne 0) {$CheckDate.Day -eq $DayOfMonth[$MonthCnt]} else {$true})) { |
| $CheckDate |
| } else { |
| |
| |
| $MonthRepeat[$MonthCnt]++ |
| } |
| } |
| |
| |
| if ($MonthRepeat[$MonthCnt] -eq -1) { |
| $j=0 |
| do { |
| $CheckDate = $CurrentDate.Date.AddMonths(-$j).AddDays($DayOfMonth[$MonthCnt]-$CurrentDate.Day) |
| $j++ |
| |
| |
| |
| |
| if ($CheckDate -le $CurrentDate -and $(if ($DayOfMonth[$MonthCnt] -ne 0) {$CheckDate.Day -eq $DayOfMonth[$MonthCnt]} else {$true})) { |
| $CheckDate |
| } |
| } while ($LimitYear -le $CheckDate.Adddays(-31).Year) |
| } |
| } |
| } |
| |
| if ($DayOfQuarter -ne $null) { |
| |
| $QuarterInt = [int](($CurrentDate.Month+1)/3) |
| $QuarterYearInt = $CurrentDate.Year |
| $QuarterLoopCount = $QuarterRepeat |
| $j = 0 |
| |
| do { |
| switch ($QuarterInt) { |
| 1 { |
| $CheckDate = ([DateTime]::ParseExact("$($QuarterYearInt)0101",'yyyyMMdd',$null)).AddDays($DayOfQuarter-1) |
| |
| |
| $DaysInFeb = ([DateTime]::ParseExact("$($QuarterYearInt)0301",'yyyyMMdd',$null)).AddDays(-1).Day |
| $DaysInCurrentQuarter = 31+$DaysInFeb+31 |
| |
| |
| if ($DayOfQuarter -gt $DaysInCurrentQuarter) { |
| $CheckDate = $null |
| } |
| |
| |
| |
| if ($DayOfQuarter -eq 0) { |
| $CheckDate = [DateTime]::ParseExact("$($QuarterYearInt)0331",'yyyyMMdd',$null) |
| } |
| |
| $QuarterInt = 4 |
| $QuarterYearInt-- |
| } |
| 2 { |
| $CheckDate = ([DateTime]::ParseExact("$($QuarterYearInt)0401",'yyyyMMdd',$null)).AddDays($DayOfQuarter-1) |
| |
| |
| $DaysInCurrentQuarter = 30+31+30 |
| |
| |
| if ($DayOfQuarter -gt $DaysInCurrentQuarter) { |
| $CheckDate = $null |
| } |
| |
| |
| |
| if ($DayOfQuarter -eq 0) { |
| $CheckDate = [DateTime]::ParseExact("$($QuarterYearInt)0630",'yyyyMMdd',$null) |
| } |
| |
| $QuarterInt = 1 |
| } |
| 3 { |
| $CheckDate = ([DateTime]::ParseExact("$($QuarterYearInt)0701",'yyyyMMdd',$null)).AddDays($DayOfQuarter-1) |
| |
| |
| $DaysInCurrentQuarter = 31+31+30 |
| |
| |
| if ($DayOfQuarter -gt $DaysInCurrentQuarter) { |
| $CheckDate = $null |
| } |
| |
| |
| |
| if ($DayOfQuarter -eq 0) { |
| $CheckDate = [DateTime]::ParseExact("$($QuarterYearInt)0930",'yyyyMMdd',$null) |
| } |
| |
| $QuarterInt = 2 |
| } |
| 4 { |
| $CheckDate = ([DateTime]::ParseExact("$($QuarterYearInt)1001",'yyyyMMdd',$null)).AddDays($DayOfQuarter-1) |
| |
| |
| $DaysInCurrentQuarter = 31+30+31 |
| |
| |
| if ($DayOfQuarter -gt $DaysInCurrentQuarter) { |
| $CheckDate = $null |
| } |
| |
| |
| |
| if ($DayOfQuarter -eq 0) { |
| $CheckDate = [DateTime]::ParseExact("$($QuarterYearInt)1231",'yyyyMMdd',$null) |
| } |
| $QuarterInt = 3 |
| } |
| } |
| |
| |
| if ($CheckDate -le $CurrentDate -and $CheckDate -ne $null) { |
| |
| |
| if ($CheckDate.Year -ge $LimitYear -and $QuarterRepeat -eq -1) { |
| $CheckDate |
| } |
| |
| |
| if ($QuarterRepeat -ne -1) { |
| $CheckDate |
| $j++ |
| } else { |
| $QuarterLoopCount++ |
| } |
| } |
| |
| } while ($(if ($QuarterRepeat -eq -1) {$LimitYear -le $(if ($CheckDate) {$CheckDate.Year} else {9999})} |
| else {$j -lt $QuarterLoopCount})) |
| } |
| |
| if ($DayOfYear -ne $null) { |
| $YearLoopCount = $DayOfYearRepeat |
| $YearInt = $CurrentDate.Year |
| $j = 0 |
| |
| |
| do { |
| $CheckDate = ([DateTime]::ParseExact("$($YearInt)0101",'yyyyMMdd',$null)).AddDays($DayOfYear-1) |
| |
| |
| if ($DayOfYear -eq 0) { |
| $CheckDate = $CheckDate.AddYears(1) |
| } |
| |
| |
| if (($DayOfYear -eq 366) -and !([DateTime]::IsLeapYear($YearInt))) { |
| $CheckDate = $null |
| } |
| |
| |
| if ($CheckDate -le $CurrentDate -and $CheckDate -ne $null) { |
| |
| if ($CheckDate.Year -ge $LimitYear -and $DayOfYearRepeat -eq -1) { |
| $CheckDate |
| } |
| |
| |
| if ($DayOfYearRepeat -ne -1) { |
| $CheckDate |
| $j++ |
| } else { |
| $YearLoopCount++ |
| } |
| } |
| $YearInt-- |
| } while ($(if ($DayOfYearRepeat -eq -1) {$LimitYear -le $(if ($CheckDate) {$CheckDate.Year} else {9999})} |
| else {$j -lt $YearLoopCount})) |
| } |
| |
| if ($DateDay -ne $null) { |
| foreach ($Date in $DateDay) { |
| try { |
| $CheckDate = [DateTime]::ParseExact($Date,'yyyy-MM-dd',$null) |
| } catch { |
| try { |
| $CheckDate = [DateTime]::ParseExact($Date,'yyyy\/MM\/dd',$null) |
| } catch {} |
| } |
| |
| if ($CheckDate -le $CurrentDate) { |
| $CheckDate |
| } |
| $CheckDate=$null |
| } |
| } |
| |
| if ($DateRange -ne $null) { |
| $CheckDate=$null |
| try { |
| $CheckDate = [DateTime]::ParseExact($DateRange,'yyyy-MM-dd',$null) |
| } catch { |
| try { |
| $CheckDate = [DateTime]::ParseExact($DateRange,'yyyy\/MM\/dd',$null) |
| } catch {} |
| } |
| if ($CheckDate) { |
| for ($k = 0; $k -lt $DateRangeRepeat; $k++) { |
| if ($CheckDate -le $CurrentDate) { |
| $CheckDate |
| } |
| $CheckDate = $CheckDate.AddDays(1) |
| } |
| } |
| } |
| } |
| |
| |
| function F_Autolog { |
| |
| $TempDate = (get-date).ToString("dd-MM-yyyy_HHmm.ss") |
| |
| $TempFolderPath = $FolderPath -replace '\\','_' |
| $TempFolderPath = $TempFolderPath -replace ':','' |
| $TempFolderPath = $TempFolderPath -replace ' ','' |
| |
| |
| $TestLogPath = Test-Path $LogFile -PathType Container |
| if (-not $TestLogPath) { |
| $LogFile = Split-Path $LogFile -Erroraction SilentlyContinue |
| } |
| if ($LogFile.SubString($LogFile.Length-1,1) -eq "\") { |
| $LogFile = $LogFile.SubString(0,$LogFile.Length-1) |
| } |
| |
| $script:LogFile = "$LogFile\Autolog_$TempFolderPath$TempDate.log" |
| } |
| |
| |
| |
| |
| function F_Deleteoldfiles { |
| $Count = $FileList.Count |
| for ($j=0;$j -lt $Count;$j++) { |
| $TempFile = $FileList[$j].FullName |
| $TempSize = $FileList[$j].Length |
| if (-not $ListOnly) {Remove-Item -LiteralPath $Tempfile -Force -ErrorAction SilentlyContinue} |
| if (-not $?) { |
| $TempErrorVar = "$($Error[0].ToString()) ::: $($Error[0].targetobject)" |
| "`tFAILED FILE`t`t$TempErrorVar" >> $LogFile |
| $script:FilesFailed++ |
| $script:FailedSize+=$TempSize |
| } else { |
| if (-not $ListOnly) { |
| $script:FilesNumber++;$script:FilesSize+=$TempSize |
| if ($VerboseLog) { |
| switch ($true) { |
| {$CreateTime} {"`tDELETED FILE`t$($FileList[$j].CreationTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| {$LastAccessTime} {"`tDELETED FILE`t$($FileList[$j].LastAccessTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| Default {"`tDELETED FILE`t$($FileList[$j].LastWriteTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| } |
| } |
| } |
| } |
| if($ListOnly) { |
| if ($VerboseLog) { |
| switch ($true) { |
| {$CreateTime} {"`tLISTONLY`t$($FileList[$j].CreationTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| {$LastAccessTime} {"`tLISTONLY`t$($FileList[$j].LastAccessTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| Default {"`tLISTONLY`t$($FileList[$j].LastWriteTime.ToString('yyyy-MM-dd hh:mm:ss'))`t$($FileList[$j].Length.ToString().PadLeft(15))`t$tempfile" >> $LogFile} |
| } |
| } else { |
| "`tLISTONLY`t$TempFile" >> $LogFile |
| } |
| $script:FilesNumber++ |
| $script:FilesSize+=$TempSize |
| } |
| } |
| } |
| |
| |
| |
| |
| function F_Checkforemptyfolder { |
| $FolderList = @($FolderList | sort-object @{Expression={$_.FullName.Length}; Ascending=$false}) |
| $Count = $FolderList.Count |
| for ($j=0;$j -lt $Count;$j++) { |
| $TempFolder = Get-ChildItem -LiteralPath $FolderList[$j].FullName -ErrorAction SilentlyContinue -Force |
| if (-not $TempFolder) { |
| $TempName = $FolderList[$j].FullName |
| Remove-Item -LiteralPath $TempName -Force -Recurse -ErrorAction SilentlyContinue |
| if(-not $?) { |
| $TempErrorVar = "$($Error[0].ToString()) ::: $($Error[0].targetobject)" |
| "`tFAILED FOLDER`t$TempErrorVar" >> $LogFile |
| $script:FoldersFailed++ |
| } else { |
| if ($VerboseLog) { |
| switch ($true) { |
| {$CreateTime} {"`tDELETED FOLDER`t$($FolderList[$j].CreationTime.ToString('yyyy-MM-dd hh:mm:ss'))`t`t`t$TempName" >> $LogFile} |
| {$LastAccessTime} {"`tDELETED FOLDER`t$($FolderList[$j].LastAccessTime.ToString('yyyy-MM-dd hh:mm:ss'))`t`t`t$TempName" >> $LogFile} |
| Default {"`tDELETED FOLDER`t$($FolderList[$j].LastWriteTime.ToString('yyyy-MM-dd hh:mm:ss'))`t`t`t$TempName" >> $LogFile} |
| } |
| } |
| $script:FoldersNumber++ |
| } |
| } |
| } |
| } |
| |
| |
| |
| if (-not $FolderPath) {Write-Warning 'Please specify the -FolderPath variable, this parameter is required. Use Get-Help .\deleteold.ps1 to display help.';exit} |
| if (-not $FileAge) {Write-Warning 'Please specify the -FileAge variable, this parameter is required. Use Get-Help .\deleteold.ps1 to display help.';exit} |
| if (-not $LogFile) {Write-Warning 'Please specify the -LogFile variable, this parameter is required. Use Get-Help .\deleteold.ps1 to display help.';exit} |
| if ($Autolog) {F_Autolog} |
| |
| |
| $Startdate = Get-Date |
| $LastWrite = $Startdate.AddDays(-$FileAge) |
| $StartTime = $Startdate.ToShortDateString()+", "+$Startdate.ToLongTimeString() |
| $Switches = "`r`n`t`t-FolderPath`r`n`t`t`t$FolderPath`r`n`t`t-FileAge $FileAge`r`n`t`t-LogFile`r`n`t`t`t$LogFile" |
| |
| if ($IncludePath) { |
| $Switches += "`r`n`t`t-IncludePath" |
| for ($j=0;$j -lt $IncludePath.Count;$j++) {$Switches+= "`r`n`t`t`t";$Switches+= $IncludePath[$j]} |
| } |
| if ($ExcludePath) { |
| $Switches += "`r`n`t`t-ExcludePath" |
| for ($j=0;$j -lt $ExcludePath.Count;$j++) {$Switches+= "`r`n`t`t`t";$Switches+= $ExcludePath[$j]} |
| } |
| if ($IncludeFileExtension) { |
| $Switches += "`r`n`t`t-IncludeFileExtension" |
| for ($j=0;$j -lt $IncludeFileExtension.Count;$j++) {$Switches+= "`r`n`t`t`t";$Switches+= $IncludeFileExtension[$j]} |
| } |
| if ($ExcludeFileExtension) { |
| $Switches += "`r`n`t`t-ExcludeFileExtension" |
| for ($j=0;$j -lt $ExcludeFileExtension.Count;$j++) {$Switches+= "`r`n`t`t`t";$Switches+= $ExcludeFileExtension[$j]} |
| } |
| if ($ExcludeDate) { |
| $Switches+= "`r`n`t`t-ExcludeDate" |
| $ExcludeDate | ConvertFrom-Csv -Header:'Item1','Item2','Item3' -ErrorAction SilentlyContinue | ForEach-Object { |
| $Switches += "`r`n`t`t`t" |
| $Switches += ($_.Item1,$_.Item2,$_.Item3 -join ',').Trim(',') |
| } |
| } |
| if ($EmailTo) { |
| $Switches += "`r`n`t`t-EmailTo" |
| for ($j=0;$j -lt $EmailTo.Count;$j++) {$Switches+= "`r`n`t`t`t";$Switches+= $EmailTo[$j]} |
| } |
| if ($EmailFrom) { |
| $Switches += "`r`n`t`t-EmailFrom`r`n`t`t`t$EmailFrom" |
| } |
| if ($EmailSubject) { |
| $Switches += "`r`n`t`t-EmailSubject`r`n`t`t`t$EmailSubject" |
| } |
| if ($EmailSmtpServer) { |
| $Switches += "`r`n`t`t-EmailSmtpServer`r`n`t`t`t$EmailSmtpServer" |
| } |
| if ($ListOnly) {$Switches+="`r`n`t`t-ListOnly"} |
| if ($VerboseLog) {$Switches+="`r`n`t`t-VerboseLog"} |
| if ($Autolog) {$Switches+="`r`n`t`t-AutoLog"} |
| if ($Appendlog) {$Switches+="`r`n`t`t-AppendLog"} |
| if ($CreateTime) {$Switches+="`r`n`t`t-CreateTime"} |
| if ($LastAccessTime) {$Switches+="`r`n`t`t-LastAccessTime"} |
| if ($CleanFolders) {$Switches+="`r`n`t`t-CleanFolders"} |
| if ($NoFolder) {$Switches+="`r`n`t`t-NoFolder"} |
| if ($RegExPath) {$Switches+="`r`n`t`t-RegExPath"} |
| [long]$FilesSize = 0 |
| [long]$FailedSize = 0 |
| [int]$FilesNumber = 0 |
| [int]$FilesFailed = 0 |
| [int]$FoldersNumber = 0 |
| [int]$FoldersFailed = 0 |
| |
| |
| if ($EmailTo -or $EmailFrom -or $EmailSmtpServer) { |
| if (($EmailTo,$EmailFrom,$EmailSmtpServer) -contains '') { |
| Write-Warning 'EmailTo EmailFrom and EmailSmtpServer parameters only work if all three parameters are used, no email sent...' |
| } else { |
| $EmailSplat = @{ |
| To = $EmailTo |
| From = $EmailFrom |
| SmtpServer = $EmailSmtpServer |
| Attachments = $LogFile |
| } |
| if ($EmailSubject) { |
| $EmailSplat.Subject = $EmailSubject |
| } else { |
| $EmailSplat.Subject = "deleteold.ps1 started at: $StartTime FolderPath: $FolderPath" |
| } |
| } |
| } |
| |
| |
| Write-Output ("-"*79) |
| Write-Output " Deleteold`t::`tScript to delete old files from folders" |
| Write-Output ("-"*79) |
| Write-Output "`n Started : $StartTime`n Folder :`t$FolderPath`n Switches :`t$Switches`n" |
| if ($ListOnly){Write-Output "`t*** Running in Listonly mode, no files will be modified ***`n"} |
| Write-Output ("-"*79) |
| |
| |
| if ($AppendLog) { |
| ("-"*79) >> $LogFile |
| } else { |
| ("-"*79) > $LogFile |
| } |
| |
| " Deleteold`t::`tScript to delete old files from folders" >> $LogFile |
| ("-"*79) >> $LogFile |
| " " >> $LogFile |
| " Started : $StartTime" >> $LogFile |
| " " >> $LogFile |
| " Folder : $FolderPath" >> $LogFile |
| " " >> $LogFile |
| " Switches : $Switches" >> $LogFile |
| " " >> $LogFile |
| ("-"*79) >> $LogFile |
| " " >> $LogFile |
| |
| |
| |
| $SelectProperty = @{'Property'='Fullname','Length','PSIsContainer'} |
| if ($CreateTime) { |
| $SelectProperty.Property += 'CreationTime' |
| } elseif ($LastAccessTime) { |
| $SelectProperty.Property += 'LastAccessTime' |
| } else { |
| $SelectProperty.Property += 'LastWriteTime' |
| } |
| if ($ExcludeFileExtension -or $IncludeFileExtension) { |
| $SelectProperty.Property += 'Extension' |
| } |
| |
| |
| Write-Output "`n Retrieving list of files and folders from: $FolderPath" |
| $CheckError = $Error.Count |
| $FullArray = @(Get-ChildItem -LiteralPath $FolderPath -Recurse -ErrorAction SilentlyContinue -Force | Select-Object @SelectProperty) |
| |
| |
| $FileList = @($FullArray | Where-Object {$_.PSIsContainer -eq $false}) |
| $FolderList = @($FullArray | Where-Object {$_.PSIsContainer -eq $true}) |
| |
| |
| |
| if ($IncludePath) { |
| |
| if (!$RegExPath) { |
| for ($j=0;$j -lt $IncludePath.Count;$j++) { |
| [array]$NewFileList += @($FileList | Where-Object {$_.FullName -match [RegEx]::Escape($IncludePath[$j])}) |
| [array]$NewFolderList += @($FolderList | Where-Object {$_.FullName -match [RegEx]::Escape($IncludePath[$j])}) |
| } |
| } else { |
| |
| for ($j=0;$j -lt $IncludePath.Count;$j++) { |
| [array]$NewFileList += @($FileList | Where-Object {$_.FullName -match $IncludePath[$j]}) |
| [array]$NewFolderList += @($FolderList | Where-Object {$_.FullName -match $IncludePath[$j]}) |
| } |
| } |
| $FileList = $NewFileList |
| $FolderList = $NewFolderList |
| $NewFileList=$NewFolderList = $null |
| } |
| |
| |
| |
| if ($ExcludePath) { |
| |
| if (!$RegExPath) { |
| for ($j=0;$j -lt $ExcludePath.Count;$j++) { |
| $FileList = @($FileList | Where-Object {$_.FullName -notmatch [RegEx]::Escape($ExcludePath[$j])}) |
| $FolderList = @($FolderList | Where-Object {$_.FullName -notmatch [RegEx]::Escape($ExcludePath[$j])}) |
| } |
| } else { |
| |
| for ($j=0;$j -lt $ExcludePath.Count;$j++) { |
| $FileList = @($FileList | Where-Object {$_.FullName -notmatch $ExcludePath[$j]}) |
| $FolderList = @($FolderList | Where-Object {$_.FullName -notmatch $ExcludePath[$j]}) |
| } |
| } |
| } |
| |
| |
| if ($IncludeFileExtension) { |
| for ($j=0;$j -lt $IncludeFileExtension.Count;$j++) { |
| |
| if ($IncludeFileExtension[$j].Substring(0,1) -ne '.') {$IncludeFileExtension[$j] = ".$($IncludeFileExtension[$j])"} |
| [array]$NewFileList += @($FileList | Where-Object {$_.Extension -like $IncludeFileExtension[$j]}) |
| } |
| $FileList = $NewFileList |
| $NewFileList=$null |
| } |
| |
| |
| if ($ExcludeFileExtension) { |
| for ($j=0;$j -lt $ExcludeFileExtension.Count;$j++) { |
| |
| if ($ExcludeFileExtension[$j].Substring(0,1) -ne '.') {$ExcludeFileExtension[$j] = ".$($ExcludeFileExtension[$j])"} |
| $FileList = @($FileList | Where-Object {$_.Extension -notlike $ExcludeFileExtension[$j]}) |
| } |
| } |
| |
| |
| |
| $CheckError = $Error.Count - $CheckError |
| if ($CheckError -gt 0) { |
| for ($j=0;$j -lt $CheckError;$j++) { |
| |
| if ($ExcludePath) { |
| if ($(for ($k=0;$k -lt $ExcludePath.Count;$k++) {$Error[$j].TargetObject -match $ExcludePath[$k].SubString(0,$ExcludePath[$k].Length-2)}) -notcontains $true) { |
| $TempErrorVar = "$($Error[$j].ToString()) ::: $($Error[$j].TargetObject)" |
| "`tFAILED ACCESS`t$TempErrorVar" >> $LogFile |
| } |
| } else { |
| $TempErrorVar = "$($Error[$j].ToString()) ::: $($Error[$j].TargetObject)" |
| "`tFAILED ACCESS`t$TempErrorVar" >> $LogFile |
| } |
| } |
| } |
| |
| |
| $AllFileCount = $FileList.Count |
| |
| |
| |
| if ($CreateTime) { |
| $FileList = @($FileList | Where-Object {$_.CreationTime -le $LastWrite}) |
| } elseif ($LastAccessTime) { |
| $FileList = @($FileList | Where-Object {$_.LastAccessTime -le $LastWrite}) |
| } else { |
| $FileList = @($FileList | Where-Object {$_.LastWriteTime -le $LastWrite}) |
| } |
| |
| |
| |
| |
| |
| if ($ExcludeDate) { |
| $SplatDate = ConvertFrom-DateQuery $ExcludeDate |
| $ExcludedDates = @(ConvertTo-DateObject @SplatDate | Select-Object -Unique | Sort-Object -Descending) |
| if ($CreateTime) { |
| $FileList = @($FileList | Where-Object {$ExcludedDates -notcontains $_.CreationTime.Date}) |
| } elseif ($LastAccessTime) { |
| $FileList = @($FileList | Where-Object {$_.LastAccessTime -le $LastWrite}) |
| } else { |
| $FileList = @($FileList | Where-Object {$ExcludedDates -notcontains $_.LastWriteTime.Date}) |
| } |
| [string]$DisplayExcludedDates = for ($j=0;$j -lt $ExcludedDates.Count;$j++) { |
| if ($j -eq 0) { |
| "`n ExcludedDates: $($ExcludedDates[$j].ToString('yyyy-MM-dd'))" |
| } else { |
| $ExcludedDates[$j].ToString('yyyy-MM-dd') |
| } |
| |
| if ((($j+1) % 6) -eq 0) {"`n`t`t "} |
| } |
| $DisplayExcludedDates |
| } |
| |
| |
| |
| |
| if ($CleanFolders) { |
| |
| } elseif ($NoFolder) { |
| $FolderList = @() |
| } else { |
| $FolderList = @($FileList | ForEach-Object { |
| Split-Path -Path $_.FullName} | |
| Select-Object -Unique | ForEach-Object { |
| Get-Item -LiteralPath $_ -ErrorAction SilentlyContinue | Select-Object @SelectProperty}) |
| } |
| |
| |
| $FullArray = "" |
| |
| |
| Write-Output "`n Files`t: $AllFileCount`n Folders`t: $($FolderList.Count) `n Old files`t: $($FileList.Count)" |
| |
| |
| if (-not $ListOnly) { |
| Write-Output "`n Starting with removal of old files..." |
| } else { |
| Write-Output "`n Listing files..." |
| } |
| F_Deleteoldfiles |
| if (-not $ListOnly) { |
| Write-Output " Finished deleting files`n" |
| } else { |
| Write-Output " Finished listing files`n" |
| } |
| if (-not $ListOnly) { |
| Write-Output " Check/remove empty folders started..." |
| F_Checkforemptyfolder |
| Write-Output " Empty folders deleted`n" |
| } |
| |
| |
| $TimeTaken = ((Get-Date) - $StartDate).ToString().SubString(0,8) |
| $FilesSize = $FilesSize/1MB |
| [string]$FilesSize = $FilesSize.ToString() |
| $FailedSize = $FailedSize/1MB |
| [string]$FailedSize = $FailedSize.ToString() |
| $EndDate = "$((Get-Date).ToShortDateString()), $((Get-Date).ToLongTimeString())" |
| |
| |
| Write-Output ($Footer = @" |
| |
| $("-"*79) |
| |
| Files : $FilesNumber |
| Filesize(MB) : $FilesSize |
| Files Failed : $FilesFailed |
| Failedfile Size(MB) : $FailedSize |
| Folders : $FoldersNumber |
| Folders Failed : $FoldersFailed |
| |
| Finished Time : $EndDate |
| Total Time : $TimeTaken |
| |
| $("-"*79) |
| "@) |
| |
| Out-File -FilePath $LogFile -Append -InputObject $Footer |
| |
| |
| if ($EmailSplat) { |
| Send-MailMessage @EmailSplat |
| } |
| |
| |
| $FileList=$FolderList = $nullCOPY |