none
Delete files from specific folder and older than X days RRS feed

  • Question

  • Hello guys,

    I'm trying to create a script that searches and delete files older than 2 days inside 2 different Root Folders' subfolders with Specific name. So far, what I did is this script below, but it is not working :(

    $limit = (Get-Date).AddDays(-2)
    $paths = "D:\work\AppLog1,G:\Logs\AppLog1" -split ','
    
    #Delete files from cdatalogs folder
    foreach($path in $paths)
    {
            Get-ChildItem -Path $path -File -Recurse | where { $_.LastWriteTime -lt $limit -and $_.FullName -contains "cdatalogs"} | Remove-Item
            Write-Host "Files from $path removed"
    }

    I don't get any error, but no file is deleted when I run the script.

    Does anyone can help me, please?

    Thank you!
    Ney Santos


     


    • Edited by KtotaC Friday, November 22, 2019 8:26 AM
    Friday, November 22, 2019 8:25 AM

All replies

  • Get-ChildItem D:\work\AppLog1\*,G:\Logs\AppLog1\* -File -Recurse -Filter *cdatalog* | 
        Where-Object{ $_.LastWriteTime -lt [datetime]::Today.AddDays(-2) } |
        Remove-Item -Verbose -WhatIf 
    


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Monday, November 25, 2019 1:02 AM
    Friday, November 22, 2019 9:49 AM
    Moderator
  • You chould change "-contains" to "-match". The operator -contains looks for a specific element in an array of elements. You are looking for a part of a string. You may read more about this in about_Comparison_Operatos.

    BTW: A better way to store a list of elements in a variable is to use an array:

    $paths = @(
        'D:\work\AppLog1'
        'G:\Logs\AppLog'
    )
    ... that's easier to read and easier to maintain.

    Live long and prosper!

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

    Friday, November 22, 2019 9:52 AM
  • Thank you for help guys, but the script is deleting files named CDATALOG. I have to delete files in CDATALOG folders instead. This is my script:

    $paths = @(
        'D:\work\AppLog1'
        'D:\work\AppLog2'
    ) -split "`t|`n|`r"
    
    #Delete files from cdatalogs folder
    foreach($path in $paths)
    {
    Write-Host $path
    
    Get-ChildItem $path -File -Recurse -Filter *cdatalog* | 
        Where-Object{ $_.LastWriteTime -lt [datetime]::Today.AddDays(-2) } |
        Remove-Item -Verbose
    
    }

    Any idea?

    Thank you!

    Monday, November 25, 2019 12:26 AM
  • Thank you for your tip!

    It is much better.

    Monday, November 25, 2019 12:27 AM
  • Think about this for a bit. Once you understand how the command and its parameters work then this becomes the obvious metod

    Get-ChildItem D:\work\AppLog1\*,G:\Logs\AppLog1\* -File -Recurse -Exclude *cdatalog* | 
        Where-Object{ $_.LastWriteTime -lt [datetime]::Today.AddDays(-2) } |
        Remove-Item -Verbose -WhatIf 

    You must use "exclude" as "Filter" does not work with files.


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Monday, November 25, 2019 1:02 AM
    Monday, November 25, 2019 12:57 AM
    Moderator
  • 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

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 25, 2019 7:57 AM
    Moderator
  • Thank you for help guys, but the script is deleting files named CDATALOG. I have to delete files in CDATALOG folders instead. This is my script:

    .....

    Any idea?

    Yes, of course. 

    BTW: You could have specified that condition in your first post a little more explicit.

    You will have to extend your filter inside the Where-Object to reflect this condition.

    Get-ChildItem D:\work\AppLog1\*,G:\Logs\AppLog1\* -File -Recurse | 
    Where-Object { 
        $_.LastWriteTime -lt [datetime]::Today.AddDays(-2) -and
        $_.PSParentPath -match '\\CDATALOG$' } |
        Remove-Item -Verbose -WhatIf


    Live long and prosper!

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


    • Edited by BOfH-666 Monday, November 25, 2019 10:37 AM
    Monday, November 25, 2019 10:37 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


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 7:19 AM
    Moderator