scan a directory tree for folder that contain "_Deleteme"

Welcome Forums General PowerShell Q&A scan a directory tree for folder that contain "_Deleteme"

Viewing 8 reply threads
  • Author
    Posts
    • #197909
      Participant
      Topics: 1
      Replies: 4
      Points: -1
      Rank: Member

      we have a directory structure (for home drives), there is an automated task that renames folders with _deleteme when it hasnt been accessed for while, we currently run through this tree periodically and delete folder with _delete me at the end.  i would like a powershell script to look through the folder tree (can be 5 or 6 sub folders here) recursivley looking for any folder with _deleteme at the end and move them (not delete) to a new folder location.

       

      Thanks in advance for you help

      Jim

    • #197921
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero

      Yes, this is possible. What have you tried? Get-ChildItem and Move-Item should get you close to your goals.

    • #197927
      Participant
      Topics: 1
      Replies: 4
      Points: -1
      Rank: Member

      Yes, this is possible. What have you tried? Get-ChildItem and Move-Item should get you close to your goals.

      Hi rob, i have tried this so far to attempt to get a list together:

      ls y: -Recurse | Select-String "_renamed_pending_deletion" | select Path, LineNumber | Format-List

    • #197930
      Participant
      Topics: 4
      Replies: 43
      Points: 64
      Helping Hand
      Rank: Member

      Is it possible that some filenames are the same?  If so, what do you want to do since two files with the same name can't exist in the same directory?

      • #197939
        Participant
        Topics: 1
        Replies: 4
        Points: -1
        Rank: Member

        these are only for foldernames only and not files, and yes they are unique.

         

        Thanks

    • #197936
      Participant
      Topics: 4
      Replies: 43
      Points: 64
      Helping Hand
      Rank: Member

      Jim, you are close.  I would recommend using a simple Where-Object instead of  Select-String to keep your file objects "in tact" on the pipeline then instead of Format-List use Move-Item to move the files.  That would work unless files have the same name, in which case you would get a non-terminating error.

    • #197942
      Participant
      Topics: 1
      Replies: 4
      Points: -1
      Rank: Member

      thanks,

      at this point im looking to report on what we have marked as this, so would like a csv file report output, would this work?

      ls y: -Recurse | Where-Object "_renamed_pending_deletion" | select Path, LineNumber | Format-List >c:\tmp\deletion.csv

    • #197948
      Participant
      Topics: 4
      Replies: 43
      Points: 64
      Helping Hand
      Rank: Member

      Your Where-Object needs to actually check a condition.

      Get-ChildItem -File -Recurse |

           Where-Object {$_.Name -like "*_Deleteme*"} |

               Format-Table -Property fullname

       

    • #197954
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero

      Look at this example:

      Get-ChildItem C:\Scripts\* -Directory -Recurse |
      Where-Object -FilterScript {$_.Name -like '*les'} |
      Export-CSV -Path C:\Report.csv -NoTypeInformation
      

      The first line recursively gets directories under C:\Scripts:

      PS C:\WINDOWS\system32> Get-ChildItem C:\Scripts\* -Directory -Recurse
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Corp                                                                                                                                                                                                            
      d-----        11/6/2019  11:16 AM                Legal                                                                                                                                                                                                           
      d-----        12/6/2019   9:04 AM                Sales                                                                                                                                                                                                           
      
      
          Directory: C:\Scripts\Sales
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        12/6/2019   9:03 AM                Inside Sales
      

      The next line filters the results to only get Folders that end in 'les' with a wildcard:

      PS C:\WINDOWS\system32> Get-ChildItem C:\Scripts\* -Directory -Recurse |
      Where-Object -FilterScript {$_.Name -like '*les'}
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        12/6/2019   9:04 AM                Sales                                                                                                                                                                                                           
      
      
          Directory: C:\Scripts\Sales
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      d-----        12/6/2019   9:03 AM                Inside Sales 
      

      Then you can output to a CSV. You can also replace this line with Move-Item and a destination to move the folder to the required location and additionally use the -WhatIf to test prior to moving folders.

    • #197960
      Participant
      Topics: 1
      Replies: 4
      Points: -1
      Rank: Member

      im now getting a csv report out, thankyou so much for your help

Viewing 8 reply threads
  • You must be logged in to reply to this topic.