locked
PowerShell script to delete files in subfolders after 28days but keep new files RRS feed

  • Question

  • Hi All,

    I have come out this script. Manage to delete the subfolder files. But unable to keep new files created today.

    Anyone have any ideal or experience what should be done.

    $Path = "C:\Testing"
    $Daysback = "-28"

    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    Get-ChildItem -Path C:\testing\abc\fig -Include *.* -File -Recurse | foreach { $_.Delete()} | Remove-Item 

    Thanks


    • Edited by Miles Tan Tuesday, December 18, 2018 8:35 AM
    Tuesday, December 18, 2018 8:34 AM

Answers

  • A couple of suggestions for all.

    To get a date offset we use integers and not strings.

    It is better to use the datetime type to do this as it is clearer and takes less code.

    $Daysback = -28 # use an integer to avoid conversion issues
    $DatetoDelete = [datetime]::Today.AddDays($Daysback)
    

    Also note that "Get-Date" returns the current time.  When we want the earliest matching date this will add all files that are stamped after the current clock time which will cause dates from beyond the cutoff date to be returned (greater than 28 days old). To avoid that use the "Today" property as it is the time at midnight.

    PS D:\scripts> [datetime]::Now
    
    Thursday, December 27, 2018 1:23:41 AM
    
    PS D:\scripts> # time normalized to the beginning of the day
    PS D:\scripts> [datetime]::Today# time now (the same as Get-Date)
    
    Thursday, December 27, 2018 12:00:00 AM
    

    Using "Today" guarantees that all requested files will be included.

    This is known in programming as a "boundary issue" which refers to the question of what a day boundary is or what any range related boundary is.


    \_(ツ)_/

    • Marked as answer by Miles Tan Tuesday, May 14, 2019 3:21 AM
    Thursday, December 27, 2018 6:28 AM
  • Please format code as code here in the forum. Use the code posting tool provided. Thanks.

    You should urgently start to learn the very basics of Powershell.

    $Path = 'C:\Testing'
    $Daysback = '-28'
    
    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    Get-ChildItem -Path $Path -File -Recurse | 
        Where-Object {$_.CreationTime -lt $DatetoDelete} |
            Remove-Item


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by ComputerScott Tuesday, December 18, 2018 9:58 PM
    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:07 AM
    Tuesday, December 18, 2018 9:00 AM
  • Hi,

    Thanks for your question.

    Your script didn't filter the file CreationTime property with $DatatoDelete. So you just use the delete function and delete all the files.

    Please refer to the script by Bofh.

    Also, you can create the task schedule to automatically run the script to delete out time files.

    https://searchitchannel.techtarget.com/feature/Using-Windows-Powershell-scripts-for-task-automation

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:11 AM
    Wednesday, December 19, 2018 5:26 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:07 AM
    Monday, December 24, 2018 2:47 AM
  • Hi Jrv,

    Thanks for your suggestion.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Tuesday, May 14, 2019 3:21 AM
    Thursday, December 27, 2018 6:40 AM

All replies

  • Please format code as code here in the forum. Use the code posting tool provided. Thanks.

    You should urgently start to learn the very basics of Powershell.

    $Path = 'C:\Testing'
    $Daysback = '-28'
    
    $CurrentDate = Get-Date
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    Get-ChildItem -Path $Path -File -Recurse | 
        Where-Object {$_.CreationTime -lt $DatetoDelete} |
            Remove-Item


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by ComputerScott Tuesday, December 18, 2018 9:58 PM
    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:07 AM
    Tuesday, December 18, 2018 9:00 AM
  • Hi,

    Thanks for your question.

    Your script didn't filter the file CreationTime property with $DatatoDelete. So you just use the delete function and delete all the files.

    Please refer to the script by Bofh.

    Also, you can create the task schedule to automatically run the script to delete out time files.

    https://searchitchannel.techtarget.com/feature/Using-Windows-Powershell-scripts-for-task-automation

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:11 AM
    Wednesday, December 19, 2018 5:26 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Thursday, December 27, 2018 6:07 AM
    Monday, December 24, 2018 2:47 AM
  • Hi Lee,

    I will Try and let you know.

    Thanks for ur kind Advise.

    Thursday, December 27, 2018 6:07 AM
  • A couple of suggestions for all.

    To get a date offset we use integers and not strings.

    It is better to use the datetime type to do this as it is clearer and takes less code.

    $Daysback = -28 # use an integer to avoid conversion issues
    $DatetoDelete = [datetime]::Today.AddDays($Daysback)
    

    Also note that "Get-Date" returns the current time.  When we want the earliest matching date this will add all files that are stamped after the current clock time which will cause dates from beyond the cutoff date to be returned (greater than 28 days old). To avoid that use the "Today" property as it is the time at midnight.

    PS D:\scripts> [datetime]::Now
    
    Thursday, December 27, 2018 1:23:41 AM
    
    PS D:\scripts> # time normalized to the beginning of the day
    PS D:\scripts> [datetime]::Today# time now (the same as Get-Date)
    
    Thursday, December 27, 2018 12:00:00 AM
    

    Using "Today" guarantees that all requested files will be included.

    This is known in programming as a "boundary issue" which refers to the question of what a day boundary is or what any range related boundary is.


    \_(ツ)_/

    • Marked as answer by Miles Tan Tuesday, May 14, 2019 3:21 AM
    Thursday, December 27, 2018 6:28 AM
  • Hi Jrv,

    Thanks for your suggestion.

    Best Regards,

    Lee


    Just do it.

    • Marked as answer by Miles Tan Tuesday, May 14, 2019 3:21 AM
    Thursday, December 27, 2018 6:40 AM