locked
Powershell Script to Delete by File Name not working RRS feed

  • Question

  • Hi All,

    Im having a problem with my powershell script to delete files by filename.

    The File name consist of :FolderName_YYYY_MM_DD_#.log

    There are hundreds of folders/sub folders

    Example is 361_2019-05-21_09.log

    E:\Folder1\361\361_2019-05-21_09.log.

    What I am attempting to do is find the oldest file by file name and delete it and keep going through folders deleting files till 200GB is free, then I would set this up as a schedule to run once a day.

    for some reason this script is not working for me:

    $now = Get-Date
    Get-ChildItem "E:\Logs -Filter '*.log' -Recurse|
    foreach {
        if ($_.basename -match "*_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_*.log)$")
        {
            $fileData = Get-Date -Year $Matches.year -Month $Matches.month -Day $Matches.day
           
            $_ | Add-Member -MemberType NoteProperty -Name 'DateFromName' -Value $fileData -PassThru
        }
    } | Where-Object {($_.DateFromName).addDays(-90) -lt $now} | Remove-Item -WhatIf -Force

    is there anyone that can help

    Tuesday, May 21, 2019 8:49 AM

All replies

  • If you use regular expressions to find some matches the asterisk does not mean what you might think it does. That means your regex pattern might not work as you expect it. I think you should change it to this: ".*_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_.*\.log)$".

    BTW: When you post code please format it as code using the code posting tool provided on the icon bar of the post editor (second to last icon). Thanks.


    Live long and prosper!

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



    Tuesday, May 21, 2019 10:06 AM
  • Hi,

    thanks for that, it got rid of the errors, but unfortunately its not deleting or even showing files with -WhatIf

    $now = Get-Date
    Get-ChildItem "D:\CTFDATA" -Filter '*.log' -Recurse| 
    foreach {
        if ($_.basename -match ".*_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_.*\.log$")
        {
            $fileData = Get-Date -Year $Matches.year -Month $Matches.month -Day $Matches.day
            
            $_ | Add-Member -MemberType NoteProperty -Name 'DateFromName' -Value $fileData -PassThru
        }
    } | Where-Object {($_.DateFromName).addDays(-3) -lt $now} | Remove-Item -force
    
    not sure what going on as its not doing anything now.


    • Edited by jjkocher Tuesday, May 21, 2019 1:18 PM
    Tuesday, May 21, 2019 1:18 PM
  • .... ooooops ... didn't pay attantion before ...  the $_.BaseName does not contain an extension ... so your regex pattern should be this "_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_" or you should use $_.Name instead of $_.BaseName.

    Live long and prosper!

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

    Tuesday, May 21, 2019 1:21 PM
  • Watch your quotes.  Incorrect quotes can have unintended results.

    Get-ChildItem D:\CTFDATA -Filter *.log -Recurse | Where-Object { if ($_.basename -match '.*_(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_.*\.log$') { $fileDate = [datetime]::New($matches.year,$matches.month,$matches.day) if($fileDate.AddDays(-3) -lt [datetime]::Now){$true}else{$false} }else{
    $false
    } } | Remove-Item -force



    \_(ツ)_/




    Tuesday, May 21, 2019 1:45 PM
  • The important lesson is that you are trying to filter a set of files.  This implies that you want to use "Where-Object" in the pipeline to filter the stream of files.


    \_(ツ)_/

    Tuesday, May 21, 2019 1:50 PM
  • 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.

    Monday, June 3, 2019 7:49 AM