Delete files older than # of days

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5254

    by Lahru at 2012-11-01 10:13:36

    I am trying to find a script that will allow me to automate the deletion of files older than 90 days & do so recursively. Although the number of days should be variable, meaning I should be able to modify the script to change that number.
    BUT I can't seem to find one that actually works. Or a method that actually works.
    I've found several on the web that claim to do just this from simple to elaborate. But when I run the script(s) with switches for reporting only such as -whatif or the parameters built into the script ... they run but return files that don't match what I believe the criterea to be.

    Example:
    $Path = "\\UNC\Path\Goes\Here"
    $DaysOld = "-90"
    $CurrentDate = Get-Date
    $DeleteIfBefore = $CurrentDate.AddDays($DaysOld)
    Get-ChildItem $Path -Recurse -exclude *.txt, *.log | Where-Object {$_.CreationTime -lt $DeleteIfBefore} | Where-Object {-not $_.PsIsContainer} | Remove-Item -WhatIf

    This ends up listing a TON of files, but when I check, by either right clicking on one of the actual files in its directory and looking at the 'Properties', it shows "Created: ?Wednesday, ?September ?12, ?2012, ??2:24:09 PM"
    Today is November 1st, 2012 ... obviously the file isn't older than 90 days.
    I at first thought that perhaps this wasn't the property that it looked at, but when I run the following I get the same result:
    PS C:\> $file = get-item "\\UNC\Path\Goes\Here\File.pdf"
    PS C:\> $file.CreationTime

    Wednesday, September 12, 2012 2:24:09 PM What am I missing? Or is there a script that actually works?

    by nohandle at 2012-11-01 10:49:01

    $Path = "c:\Temp"
    $DaysOld = "-90"
    $CurrentDate = Get-Date
    $DeleteIfBefore = $CurrentDate.AddDays($DaysOld)
    Get-ChildItem $Path -Recurse -exclude *.txt, *.log |
    Where-Object {$_.CreationTime -lt $DeleteIfBefore} |
    Where-Object {-not $_.PsIsContainer}|
    sort CreationTime -Descending |
    select CreationTime, name -First 1 | ft -AutoSize

    CreationTime Name
    ------------ ----
    15.6.2012 10:24:57 SDI.xml

    did not change the code.
    Looks ok to me.

    by nohandle at 2012-11-01 10:52:31

    It also works for me on UNC paths. With powershell v2 and v3.

    by mikefrobbins at 2012-11-01 10:53:52

    Your script appeared to work correctly for me, however, I would use the LastWriteTime property instead of the CreationTime unless you have a specific reason for doing so. Here's a modified version of your script. By placing the two parameters you need into a param block, you can specify those values when calling the script without having to modify it each time. I've also consolidated the two where-objects into a single compound where-object.

    param (
    $Path = "\\UNC\Path\Goes\Here",
    $DaysOld = 90
    )
    Get-ChildItem $Path -Recurse -exclude *.txt, *.log |
    Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-$DaysOld) -and $_.PsIsContainer -eq $false}

    The script can be piped to Remove-Item to achieve your desired results. Test with the -WhatIf parameter as you did with your script.

    .\My-Script.ps1 -Path "\\UNC\Path\Goes\Here" -DaysOld 90 | Remove-Item -WhatIf

    by mikefrobbins at 2012-11-01 11:59:08

    Although I couldn't reproduce the issue, my guess is that the issue you ran into is because placing "-90" inside quotes makes PowerShell see that value as a string. Try removing the quotes and PowerShell should see -90 as an int32.

    by Lahru at 2012-11-02 07:04:21

    @ mikefrobbins: Your changes seem to work well, I'll have to keep an eye on this.
    What would be the best way to also log what is getting deleted?

    by nohandle at 2012-11-02 07:56:34

    [quote="mikefrobbins"]
    I would use the LastWriteTime property instead of the CreationTime unless you have a specific reason for doing so.
    [/quote]
    Use creation time if you want to remove files that were created before 90 days, use the LastWriteTime if you want to remove files that has not been written in 90 days.
    I do agree that in this situation the intent of the scripter was probably to do the latter. But your explanation seemes to me like "The values hold the same information but the one can be locked sometimes. So use the another one instead.".

    [quote="mikefrobbins"]
    my guess is that the issue you ran into is because placing "-90" inside quotes makes PowerShell see that value as a string
    [/quote]
    I don't think the issue lies here. the "-90" is stored as string but the add days does implicit conversion to double. so this should be no issue.
    Nice adaptation of the script btw!

    [quote="Lahru"]
    What would be the best way to also log what is getting deleted?
    [/quote]
    -Edit: I am WRONG output is not redirected to the standard output in this case- try adding -verbose parameter to the remove item for extra info on deletions. and you can use out-file with -append parameter for saving the info .

    by mikefrobbins at 2012-11-02 08:15:54

    [quote="Lahru"]@ mikefrobbins: Your changes seem to work well, I'll have to keep an eye on this.
    What would be the best way to also log what is getting deleted?[/quote]

    You could log the results to a file by using the Tee-Object cmdlet. The following assumes that you've saved my previous script as "My-Script.ps1".

    .\My-Script.ps1 -Path "\\UNC\Path\Goes\Here" -DaysOld 90 | Tee-Object -FilePath d:\logs\removedFiles.txt | Remove-Item

    If you did it this way, the files would be deleted and the log file would be empty:

    .\My-Script.ps1 -Path "\\UNC\Path\Goes\Here" -DaysOld 90 | Remove-Item | Out-File d]

    Test with the -WhatIf parameter and then remove it if your testing achieves the desired results.

    by nohandle at 2012-11-02 09:11:06

    [quote="mikefrobbins"]
    If you did it this way, the files would be deleted and the log file would be empty:
    .\My-Script.ps1 -Path "\\UNC\Path\Goes\Here" -DaysOld 90 | Remove-Item | Out-File d]
    [/quote]
    you are right, I was wrong. never used remove-item this way, because every time I log output I do it myself while error checking. thanks.

    on the other hand with the tee-object you get the file logged even if the file is not deleted in the end.

You must be logged in to reply to this topic.