none
Forefiles: possible to delete after x days based on number of files? RRS feed

  • Question

  • Hi, I've searched around a bit, but haven't found a solution for my particular problem.

    We do workstation image backups which are saved to a NAS device. One full backup a week and differentials on the remaining weekdays. I'd like to always have at least two full backups present, regardless of the age. This is in case something gets weird with the scheduled task or connection to the NAS which prevents the backups from occurring. This way I don't end up in a situation where a month has gone by without a backup, but the forfiles batch has been deleting everything and the folder is empty. Not an ideal situation. 

    I want to delete files older than 14 days using forfiles, which I already have a working batch script for. That part is done.

    The tricky part is, I would like the above batch to run only if a certain amount of files exist. Something along the lines of:

    if equal to or more than 3 of a certain file type exist, then run forfiles script that deletes files older than 14 days.

    Is this beyond what cmd commands and forfiles can do? Seems simple, but i'm not seeing it. I'm not a scripting pro, so I'd like to keep it simple. Other ideas besides using forfiles would be appreciated! Thank you.

    Thursday, May 1, 2014 4:20 PM

Answers

  • Something like this in PowerShell would work:

    #Requires -Version 3.0
    $Files = Get-ChildItem C:\Backup -File
    If ($Files.count -gt 3) {
        $Files | Where {
            $_.LastWriteTime -lt (Get-Date).AddDays(-14)
        } | Remove-Item
    }
    

    Or for 2.0

    #Requires -Version 2.0
    $Files = Get-ChildItem C:\Backup | Where {
        -Not $_.PSIsContainer
    }
    If ($Files.count -gt 3) {
        $Files | Where {
            $_.LastWriteTime -lt (Get-Date).AddDays(-14)
        } | Remove-Item
    }


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book


    Thursday, May 1, 2014 4:54 PM
    Moderator

All replies

  • If you're not opposed to using Windows PowerShell, then you could easily combine some PowerShell code that will run your forfile command(s). One of the great things about Windows PowerShell is that it can natively run other Windows command line programs. This example will only run the forfile command(s) if the count of files in C:\Backups is 3 or greater.

    If ((Get-ChildItem -Path 'C:\Backups').Count -ge 3) {
        #Insert forfile command(s) here
    }


    Thursday, May 1, 2014 4:47 PM
  • Something like this in PowerShell would work:

    #Requires -Version 3.0
    $Files = Get-ChildItem C:\Backup -File
    If ($Files.count -gt 3) {
        $Files | Where {
            $_.LastWriteTime -lt (Get-Date).AddDays(-14)
        } | Remove-Item
    }
    

    Or for 2.0

    #Requires -Version 2.0
    $Files = Get-ChildItem C:\Backup | Where {
        -Not $_.PSIsContainer
    }
    If ($Files.count -gt 3) {
        $Files | Where {
            $_.LastWriteTime -lt (Get-Date).AddDays(-14)
        } | Remove-Item
    }


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book


    Thursday, May 1, 2014 4:54 PM
    Moderator
  • Hi Sherman,

    I'd recommend moving away fram batch and towards Windows Powershell. If you're not well versed with scripting, it'd make more sense to start with somethig fairly current. Learning the basics will allow you to handle this (and many other challenges) with ease. These commands may be of help to you:

    • Get-Help
    • Get-ChildItem
    • Where-Object (Where)
    • Remove-Item

    You can save the result of a command into a variable and reuse that variable like this:
    $NameOfVariable = Get-ChildItem C:\ExampleFolder

    There's an entire tutorial section here in the scriptcenter and you can check out some scripts in the gallery to see how others went about scripting.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, May 1, 2014 4:55 PM
  • Thanks for the replies. This was all very helpful. I appreciate the scripts and the advice. I'll check Powershell out a bit.
    Thursday, May 1, 2014 7:30 PM