scan a directory tree for folder that contain "_Deleteme"

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

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

[quote quote=197921]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

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?

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.

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

 

Thanks

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

Your Where-Object needs to actually check a condition.

Get-ChildItem -File -Recurse |

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

Format-Table -Property fullname

 

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.

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