Delete Files older then X with Log

Hi all,

I have found a lot about that topic but nothing was solving my Problem maybe you can help me. The Target of the script is to clean up a Public share drive with Files they are older then 2day's by CreationTime. After it checks also for empty folder and delete it as well.

That was the "easy" part for me. Now to have a controlling on the job i would like to have a log File. The Schedule Task once per day. The Script generate a logfile with the name of the date.

The Issue what i'm facing now, is to get a soloution to count all the failed / successfully deleting of files. I would like to see how many went well and how many he could not delete maybe beceause the File was corrupt or there was a session open o the file.

# all Variable
$limit = (Get-Date).AddDays(-2)
$count = 0
$FilesFailed = 0
$FilesSuccessfully = 0
$Files = Get-ChildItem $path -Name
$Totalfiles = Get-ChildItem $path -Recurse
$path = "C:\temp\ScriptTest"


# Delete files older than 2Day's
Get-ChildItem -Path $path -Recurse -Force | 
Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | 
ForEach-Object{$count++} |
Remove-Item -Force -WhatIf
$TotalFilesLog = +$TotalFiles.Count 

# Write footer to log and output to console
Write-Output ($Logfile = @"
          
$("-"*79)

   
   Total Files         : $TotalFileslog
   Files to delete     : $Count
   Files Failed        : $FilesFailed
   Files Successfully  : $FilesSuccessfully

   Finished Time       : $EndDate
 

$("-"*79)
"@) | Out-File -FilePath "C:\temp\$(get-date -f yyyy-MM-dd).txt"

# Delete any empty directories left behind after deleting the old files
Get-ChildItem -Path $path -Recurse -Force | 
Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force |
Where-Object { !$_.PSIsContainer }) -eq $null } | 
Remove-Item -Force -Recurse -WhatIf 

Thanks a lot for help or clous to get it done.

Regards,

Ari


  • Edited by Psyclos 23 hours 42 minutes ago
February 5th, 2014 6:50am

You can use try-catch-finally here.

Read about_Try_Catch_Finally 

Below is a sample of how to do it:

$files = Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit }

foreach ($file in $files)
{

    try
    {
       Remove-Item -Path $file -ErrorAction Stop -ErrorVariable $FileDeleteError
    
    }
    catch
    {
        Write-Verbose -Message "Couldn't delete file $($file.name) "
        $file | Out-File -Append -FilePath C:\ErrorDeletefile.txt #put the file name into a log file
        $FileDeleteError.exception
    }
}

Whenever the Remove-Item is not able to delete file the control executes the code inside Catch block where you can log it

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2014 7:43am

Hi Thank you a lot, i will try that as well.

i found know also an other soloution, maybe it looks for a powershell pro ugly like hell, but i sjust started with ps and bit proud that it works.

do you have maybe an easier way to merge the log?

# all Variable
$limit = (Get-Date).AddDays(-2)
$count = 0
$FilesFailed = 0
$FilesSuccess = 0
$Files = Get-ChildItem $path -Name
$Totalfiles = Get-ChildItem $path -Recurse
$path = "c:\temp\"


# Counting all Files
$TotalFilesLog = +$TotalFiles.Count

# Counting Files to delete
Get-ChildItem -Path $path -Recurse -Force | 

Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | 
ForEach-Object {$count++}

# Delete files older than 2Day's
Get-ChildItem -Path $path -Recurse -Force | 
Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } |

ForEach-Object {
 $_ |
Remove-Item -Force -WhatIf
if ($_)
{
        "Fail $_"
        $FilesFailed = $FilesFailed+1
    } 
    else
    {
        "Success $_"
        $FilesSuccess = $FilesSuccess+1
    } 
    }  | Out-File -FilePath "C:\temp\FS_Report $(get-date -f yyyy-MM-dd).txt"


# Write footer to log and output to console
Write-Output ($Logfile = @"
          
$("-"*79)

   
   Total Files         : $TotalFileslog
   Files to delete     : $Count
   Files Failed        : $FilesFailed
   Files Success       : $FilesSuccess

   Finished Time       : $EndDate
 

$("-"*79)
"@) | Out-File -FilePath "C:\temp\Summary.txt"



# Create Summery of all log files
New-Item -ItemType file "C:\temp\Summary $(get-date -f yyyy-MM-dd).txt"
$file1 = Get-Content "C:\temp\FS_Report $(get-date -f yyyy-MM-dd).txt"
$file2 = Get-Content "C:\temp\Summary.txt"
Add-Content "C:\temp\Summary $(get-date -f yyyy-MM-dd).txt" $file1
Add-Content "C:\temp\Summary $(get-date -f yyyy-MM-dd).txt" $file2


# Delete any empty directories left behind after deleting the old files
Get-ChildItem -Path $path -Recurse -Force | 
Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force |
Where-Object { !$_.PSIsContainer }) -eq $null } | 
Remove-Item -Force -Recurse -WhatIf 

February 5th, 2014 8:40am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics