PS - 查找一段时间内未修改文件的文件夹

PS - Find Folders that Haven't Had their Files Modfied in Some Time

我们正在迁移我们的 FTP,我只想迁移其中包含在过去 6 个月内 used/written 的文件的文件夹。我认为这是我在 google 到处都能找到的东西,但我发现的所有脚本都有同样的致命缺陷。

似乎我找到的所有内容都取决于文件夹的“修改日期”。问题是,我有很多文件夹显示多年前的“修改日期”,但是当你深入研究它时,有一些文件是最近才创建和写入的。

示例: D:/Weblogs 可能会显示 01/01/2018 的修改日期,但是,当你深入研究它时,有一些文件夹 idk,假设名为“Log6”,并且该文件夹中有一个日志文件被修改为最近和昨天一样。

我看到的所有这些脚本都会提取顶级文件夹的修改日期,这似乎不准确。

有什么办法解决这个问题吗?我会期待类似的东西 获取某个顶层的所有文件夹,然后通过这些文件夹的 CONTENTS 查找具有 datemodified -lt adddays(-180) 过滤器的文件。如果找到“新”的东西,则不要将总体目录添加到数组中,但如果没有,则列出目录。

有什么想法吗?

编辑:我已经试过了

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

$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

试一试,我添加了评论供您遵循思考过程。

-Force的用途主要是寻找隐藏的文件和文件夹

$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

如果您需要查找最近没有修改文件的文件夹,您可以使用 $folders 数组并将其与 $result:

进行比较
$folders.where({$_.FullName -notin $result.FullName}).FullName