Find and delete files that DON'T contain text in filename

Welcome Forums General PowerShell Q&A Find and delete files that DON'T contain text in filename

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
2 months, 4 weeks ago.

  • Author
    Posts
  • #108907

    Participant
    Points: 1
    Rank: Member

    I've been banging my head on this one so any help is appreciated.

    Desired Outcome: Automate the deletion of files that don't contain specific text in the filename.

    Strategy: Create an array of filenames in a folder.  Create an array of numbers from a text file.  Identify filenames that contain no instance of the numbers from the text file.  Delete those files.

    Code so far:

    # https://stackoverflow.com/questions/18616581/how-to-properly-filter-multiple-strings-in-a-powershell-copy-script
    
    # https://social.technet.microsoft.com/Forums/en-US/cdab4808-eb91-4df3-841b-92075a6fa0fb/creating-an-array-from-the-getchilditem-cmdlet?forum=ITCG
    
    # https://stackoverflow.com/questions/13998777/storing-directory-folder-names-into-array-powershell
    
    # Put the policy files into an array called "Files"
    
    $PolicyPath = "C:\ProgramData\LANDesk\Policies"
    
    #[array]$Files = @(Get-ChildItem -Path $PolicyPath\* -Include *.stat, *.xml | Select-Object -ExpandProperty fullname)
    
    [array]$Files = @(Get-ChildItem -Path $PolicyPath\* -Include *.stat, *.xml | Foreach-Object {$_.Name})
    
     
    
    # https://sharepoint.stackexchange.com/questions/104056/contents-of-file-into-an-array-powershell
    
    # Put the list of active tasks into an array called "ActiveTasks"
    
    $ActiveTasksPath = "\\server\share"
    
    $ActiveTasks = [string[]](Get-Content -Path $ActiveTasksPath\ActiveTasks.txt)
    
     
    
     
    
    # Testing matching
    
    $ActiveTasks -match ($Files -join "|")
    
    $ActiveTasks -match $Files[135] | Foreach-object { "$_ should be kept" }
    
     
    

    The result of the matching above turns out all the numbers in $ActiveTasks.

    I've tried other comparison and they didn't work either.

     

    Thanks for reading my post!

     

     

     

  • #108916

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    Try below code,

    $ActiveTasks = (Get-Content -Path \\server\share\ActiveTasks.txt ) -join "|" 
    $FilesToDelete = Get-ChildItem -Path $PolicyPath\* -Filter *.stat,*.xml | Where-Object -FilterScript { $_.BaseName -notmatch $ActiveTasks }
    
    
  • #108919

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    I've never had much luck with -Include, but if I did something like this:

    $Files = Get-ChildItem -Path 'C:\Scripts\*.xml','C:\Scripts\*.stat'
    

    I have 3 stat files...

    PS C:\WINDOWS\system32> $Files = Get-ChildItem -Path 'C:\Scripts\*.xml','C:\Scripts\*.stat'
    
    PS C:\WINDOWS\system32> $Files
    
    
        Directory: C:\Scripts
    
    
    Mode                LastWriteTime         Length Name                                                                                                                                                                  
    ----                -------------         ------ ----                                                                                                                                                                  
    -a----        8/17/2018   4:08 PM              0 friday123.stat                                                                                                                                                        
    -a----        8/17/2018   4:08 PM              0 saturday123.stat                                                                                                                                                      
    -a----        8/17/2018   4:08 PM              0 thursday123.stat   
    

    Let's say I wanted to delete files that were not saturday, because I get to sleep in:

    PS C:\WINDOWS\system32> $files | Where{$_.BaseName -notlike 'saturday*'}
    
    
        Directory: C:\Scripts
    
    
    Mode                LastWriteTime         Length Name                                                                                                                                                                  
    ----                -------------         ------ ----                                                                                                                                                                  
    -a----        8/17/2018   4:08 PM              0 friday123.stat                                                                                                                                                        
    -a----        8/17/2018   4:08 PM              0 thursday123.stat   
    

    To remove the files, you would just pipe them to Remove-Item

    $filesToDelete = Get-ChildItem -Path 'C:\Scripts\*.xml','C:\Scripts\*.stat' |
                     Where-Object -FilterScript {$_.BaseName -notlike 'saturday*'}
                
    
    $filesToDelete | Remove-Item -Force -WhatIf
    

You must be logged in to reply to this topic.