Home > Software design >  PS - Find Folders that Haven't Had their Files Modfied in Some Time
PS - Find Folders that Haven't Had their Files Modfied in Some Time

Time:01-06

We're migrating our FTP and I would like to only migrate folders that have had files in them that have been used/written in the last 6 months. I would think this would be something that I find all over the place with google, but all the scripts I've found have the same fatal flaw.

It seems with everything I find, it depends on the "Date modified" of the folder. The problem with that is, I have PLENTY of folders that show a "Date Modified" of years ago, yet when you dig into it, there are files that are being created and written as recently as today.

Example: D:/Weblogs may show a date modified of 01/01/2018 however, when you dig into it, there is some folder, idk, called "Log6" let's say, and THAT folder has a log file in it that was modified as recently as yesterday.

All these scripts I'm seeing pull the date modified of the top folder, which just doesn't seem to be accurate.

Is there any way around this? I would expect something like Get all folders at some top level, then foreach through the CONTENTS of those folders looking for files with the datemodified -lt adddays(-180) filter. If stuff that's "New" is found, then don't add the overarching directory to the array, but if not, then list the directory.

Any ideas?

Edit: I've tried this

$path = <some dir>
gci -path $path -Directory where-object {LastWriteTime -lt (get-date).addmonths(-6))} 

and

$filter = {$_.LastWriteTime -lt (Get-Date).AddDays(-180)}
#$OldStuff = gci "D:\FTP\BELAMIINC" -file | Where-Object $filter
$OldFolders = gci "D:\FTP\BELAMIINC" -Directory | ForEach-Object {
    gci "D:\FTP\BELAMIINC" -file | Where-Object $filter
}

Write-Host $OldFolders

CodePudding user response:

Give this a try, I added comments for you to follow along the thought process.

The use of -Force is mainly to find hidden files and folders.

$path = '/path/to/parentFolder'
$limit = [datetime]::Now.AddMonths(-6)

# Get all the child folders recursive
$folders = Get-ChildItem $path -Recurse -Directory -Force

# Loop over the folders to see if there is at least one file
# that has been modified or accessed after the limit date
$result = foreach($folder in $folders)
{
    :inner foreach($file in Get-ChildItem $folder -File -Force)
    {
        if($file.LastAccessTime -gt $limit -or $file.LastWriteTime -gt $limit)
        {
            # If this condition is true at least once, break this loop and return
            # the folder's FullName, the File and File's Date for reference
            [pscustomobject]@{
                FullName       = $folder.FullName
                File           = $file.Name
                LastAccessTime = $file.LastAccessTime
                LastWriteTime  = $file.LastWriteTime
            }
            break inner
        }
    }
}

$result | Out-GridView

If you need to find the folders that don't have files recently modified you can use the $folders array and compare it against $result:

$folders.where({$_.FullName -notin $result.FullName}).FullName
  •  Tags:  
  • Related