Loop for first match of LastWriteTime

Goal of script is to ID Archive ready folders Not specific files but any RootFolder that has NO current data

T:\RootFolder1 -> GCI through that folder - the first file w/ LastWriteTime -ge (get.date).AddDays(-1095)

spit out just “RootFolder1” to an array “Current”. So if T:\RootFolder1\Test\Temp\recentfile.docx was written to in the past 3years the entire Folder path T:\RootFolder1* considered current. And “RootFolder1” is output to array “current”

else if it gets to the end of that RootFolder1 -recurse and no files have a LastWriteTime in the past three years. Then “RootFolder1” is output to array “archive”

=============================================

This is what I am trying to do. I am also new to PS and determined to learn. So all I am asking is broad logic for me to figure out steps. Problem I am having is it does not break on first match. I think I need to incorporate GCI into a while loop? Two issues I am having is breaking out of the loop for each RootFolderN. And moving on to next RootFolderN

$archivedate = (get-date).AddDays(-1095)
$rootFolder = gci -Attributes D -Depth 0
$deepsearch = gci -Attributes !D -Recurse -file | Where-Object {$_.LastWriteTime -ge $archive}
$archiveFolders = @()
$currentFolders = @()

foreach ($File in $rootFolder) - No working as it forces iteration through all - does not break out on first match? Is that correct assumption?

Thank you,

 

 

Welcome to Powershell.org.

When you post code or example data or console output please format it as code using the code tag button labeled “PRE”. Thanks

In your code you define a variable $archivedate but later on you’re using a variable $archive.

The following snippet could be one approach to get what you want:

$startFolder = ‘C:\Windows’
$threeYearsAgo = (Get-Date).AddYears(-3)
$rootFolderList = Get-ChildItem -Path $startFolder -Directory
$currentFolderList = foreach ($rootfolder in $rootFolderList) {
Get-ChildItem -Path rootFolder.FullName -File -ErrorAction SilentlyContinue | ForEach-Object { if (_.LastWriteTime -ge $threeYearsAgo) {
$rootFolder
continue
}
}
}
$archiveFolderList = Compare-Object -ReferenceObject $rootFolderList -DifferenceObject $currentFolderList -Property Name -PassThru

$currentFolderList
$archiveFolderList

Regardless of all that - please do yourself and especially all people willing to help you a favour and do not use aliasses in scripts and in forums. That will make your code much easier to read, easier to understand and easier to maintain. You might take a little time to read the The Unofficial PowerShell Best Practices and Style Guide.

Thank you - I am reading over the Best Practices. Not only helpful to community coherence but it has been helpful in my understanding on the relational aspects of PS.