使用 PowerShell 查找共享的所有顶级目录中所有文件/子文件夹的最新修改日期

MDM*_*rra 5 powershell

我在 Windows 2003 文件服务器上有一个共享目录树,其中包含大约 100GB 的数据。我需要找到此共享中的所有顶级目录,其中每个子文件夹中的每个文件的最后修改时间自 2011 年 1 月 1 日起尚未修改。本质上,我正在寻找被放弃的股票。

目录结构看起来像这样:

-a
--a1
--a2
--a3
----a3_1

-b
--b1
--b2

-c
--c1
----c1_1

etc
Run Code Online (Sandbox Code Playgroud)

我想要做的是查明a、b 或 c 下所有非隐藏文件的修改日期是否在 2011 年 1 月 1 日之前或之后。

到目前为止,我可以通过以下方式找到每个文件一年后的修改时间:

get-childitem "\\server\h$\shared" -recurse | where-object {$_.mode -notmatch "d"} |
where-object {$_.lastwritetime -lt [datetime]::parse("01/01/2011")}
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做是单独检查每个顶级目录,看看其中包含的所有文件是否都超过一年。有任何想法吗?

jsc*_*ott 5

我认为您要求仅查看文件修改时间。不确定您想要对文件夹执行什么操作,该文件夹仅包含一年内未修改的子文件夹。我也不确定“每个顶级目录”是否是指a,,或,,b...caa1a2

下面查看所有目录,并且仅列出不包含过去一年内写入的文件的目录。让我知道这是否会产生您正在寻找的输出:

$shareName = "\\server\share"
$directories = Get-ChildItem -Recurse -Path $path | Where-Object { $_.psIsContainer -eq $true }

ForEach ( $d in $directories ) { 
    # Any children written in the past year?
    $recentWrites = Get-ChildItem $d.FullName | Where-Object { $_.LastWriteTime -gt $(Get-Date).AddYears(-1) } 
    If ( -not $recentWrites ) {
        $d.FullName
    }
}
Run Code Online (Sandbox Code Playgroud)

根据您的评论进行编辑。如果您只想获取不包含过去一年修改的文件的顶级目录,请尝试以下操作。请注意,对于非常深/大的份额,这可能需要一些时间才能运行。

$shareName = "\\server\share"
# Don't -recurse, just grab top-level directories
$directories = Get-ChildItem -Path $shareName | Where-Object { $_.psIsContainer -eq $true }
ForEach ( $d in $directories ) { 
    # Get any non-container children written in the past year
    $recentWrites = Get-ChildItem $d.FullName -recurse | Where-Object { $_.psIsContainer -eq $false -and $_.LastWriteTime -gt $(Get-Date).AddYears(-1) } 
    If ( -not $recentWrites ) {
        $d.FullName
    }
}
Run Code Online (Sandbox Code Playgroud)