Return folder name only if All files of the folder or sub-folder qualify date

This works fine and finds files 365 days and older, including sub-folders, grouped by newest 5, sorted by oldest to newest.

I'd like to change this to return each folder name only if all the files each folder qualifies, i.e. is 365 days or older.

As it is, some or all files of a folder may or may not qualify, i.e. some may be older or newer than 365. I'd like it qualify and return just that folder or sub-folder name if all the files in that folder or sub-folder are 365 days or older.

$SrcDir = 'c:\local'
$nDaysOld = 365
$nCount = 5
$nItemsPerGroup = 5
$fnSrchPattern = "*.*"
$fnRslts = "c:\local\FilesNDaysOrOlder5Each.csv"

Get-ChildItem $SrcDir -recurse -include @($fnSrchPattern) | 
	Where-Object { $_.LastWriteTime -le (Get-Date).adddays(-$nDaysOld) } |
	Group-Object Directory |
	ForEach-Object {
	$_ |
	select-object -ExpandProperty Group | sort-object -property LastWriteTime | Select-Object -First $nItemsPerGroup |
	select-object -Property Directory, Name, LastWriteTime, Length
	} |
	Export-CSV -notypeinformation $fnRslts

Open in new window

LVL 26
NVITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daryl BamforthTechnical ExpertCommented:
You need to build loops within loops. First you need to recursively return directories only ($folders = get-childitem -directory -recurse)

Then for each of these get-childitem -file  run the age checks again but with an if statement in the middle that adds a check, Then we want to ensure that this check didn't actually get touched (it only needs to be true once in each run to fail). Then we are left with every folder over 365 in a clean array.

The script below returns every directory path that matches and writes out to a text file. If you need to only return the top 5, then just add your other qualifiers to $folderover365.

$srcdir = 'c:\local'
$nDaysOld = 365
$fnRslts = "c:\local\FoldersAllFilesOver365Days.txt"

$folders = get-childitem -path $srcdir -directory -recurse
$folderover365 = @()
foreach ($folder in $folders) {
    $files = get-childitem -file $folder.PSPath 
    $marker = $false
    foreach ($file in $files) {

        if ($file.lastwritetime -gt (Get-Date).adddays(-$nDaysOld)) {
            $marker = $true
        } 
    }
    if (!($marker)) {
        $folderover365 +=  $folder
    }
}
$folderover365 |select -expandproperty fullname |out-file $fnRslts

Open in new window

1
NVITAuthor Commented:
Daryl,

Thanks for your help. It works for the most part. But, an issue with folders. It returns folders that are empty, i.e. it has no files, or it has no files but has sub-folders (which may or may not have qualified files).

I'd like to skip those types of folders, i.e. don't return those.
0
Daryl BamforthTechnical ExpertCommented:
No problem, we can just wrap the guts of it in another if statement that only enters if $files has data in it.

Try this one.

$srcdir = 'c:\local'
$nDaysOld = 365
$fnRslts = "c:\local\FoldersAllFilesOver365Days.txt"

$folders = get-childitem -path $srcdir -directory -recurse
$folderover365 = @()
foreach ($folder in $folders) {
    $files = @()
    $files = get-childitem -file $folder.PSPath 
    $marker = $false
    if ($files) {
        foreach ($file in $files) {

            if ($file.lastwritetime -gt (Get-Date).adddays(-$nDaysOld)) {
                $marker = $true
            } 
        }
        if (!($marker)) {
            $folderover365 +=  $folder
        }
    }
}
$folderover365 |select -expandproperty fullname |out-file $fnRslts

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITAuthor Commented:
Sweet! Works perfectly.

I'm learning a lot by stepping through the code in the debugger and observing the changes.

Thank you, Daryl. I appreciate your help.
Have a nice day...
0
Daryl BamforthTechnical ExpertCommented:
Glad I could help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.