Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm currently using the Powershell script below to delete all files directories and sub directories of "$dump_path" that are seven days or older based upon the creation date and not modified date.

The problem with this script is that if folder "A" is seven (or more) days old it will be deleted even if its sub folders and files are less then seven days old.

What I would like this script to do is delete all files from the root and in all sub folders of "$dump_path" that are seven or more days old but maintain the parent folder(s) of files and folders that are less than seven days old even if that means the parent folders are more than seven days old. If all subfolders and files are seven days or older than the parent folder then the parent can be deleted.

Slightly obscure problem I know, but the intention is to have a 7 day retention period of all data in a 'sandbox' location of our shared areas.

Also, an added bonus if it could generate a log of what it deletes and e-mails it out post deletion.

# set folder path
$dump_path = "c:\temp"

# set minimum age of files and folders
$max_days = "-7"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files and folders
Get-ChildItem $dump_path   | Where-Object { $_.CreationTime -lt $del_date }  | Remove-Item -Recurse
share|improve this question
    
Any particular reason for you to unaccept my answer after more than 6 months? –  Ansgar Wiechers May 16 '13 at 13:02

2 Answers 2

up vote 6 down vote accepted

May I suggest a slightly different approach? I'd delete all files older than 7 days first, and in a second step delete empty folders. Something like this:

$deleted = @()

Get-ChildItem $dump_path -Recurse | Where-Object {
  -not $_.PSIsContainer -and $_.CreationTime -lt $del_date
} | ForEach-Object {
  $deleted += $_.FullName
  $_
} | Remove-Item

function Remove-EmptyFolders($folder) {
  Get-ChildItem $folder | Where-Object { $_.PSIsContainer } | ForEach-Object {
    $path = $_.FullName
    Remove-Emptyfolders $path
    if ( @(Get-ChildItem $path -Recurse | Where-Object { -not $_.PSIsContainer}).Length -eq 0 ) {
      $deleted += $path
      Remove-Item $path -Recurse -Force
    }
  }
}

Remove-EmptyFolders $dump_path

$html = $deleted | Select-Object @{Name='Path';Expression={$_}} | ConvertTo-Html
share|improve this answer
    
I'd use an array, append the paths of the deleted objects to it, and then convert the array to HTML. See edited post (you need Select-Object to create objects from the strings, so ConvertTo-Html can work with them). For sending e-mail from PowerShell see this blog post. –  Ansgar Wiechers Sep 9 '12 at 21:02
    
Not sure what you mean. ConvertTo-Html creates a table from the array with one row per per array element. –  Ansgar Wiechers Sep 10 '12 at 10:13

Thank you so much for the code. It has been a great help.

I added the following to send an email with the deletion results:

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

$html = $deleted | Select-Object @{Name='Path';Expression={$_}} | ConvertTo-Html -Head $style

#SMTP server name
 $smtpServer = "smtp.xxxx.com"

 #Creating a Mail object
 $msg = new-object Net.Mail.MailMessage

 #Creating SMTP server object
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)

 #Email structure 
 $msg.From = "email@xxxx.com"
 #$msg.ReplyTo = "replyto@xxxx.com"
 $msg.To.Add("email@xxxx.com")
 $msg.subject = "Deleted files and folders"
 $msg.IsBodyHTML = $true
 $msg.body = $html

 #Sending email 
 $smtp.Send($msg)

I did notice that the email generated only contained a list of the files that were deleted, not the folders, so haven't worked out why the folders aren't listed (if you have any ideas that would be great).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.