2

我在不同的文件夹映射到 X 驱动器中有近 500k 的 CCTV 文件,我需要在特定时间删除所有这些文件,例如仅从午夜 12 点到早上 6 点,但无论日期如何都保留剩余的文件。我怎样才能做到这一点?我尝试使用文件,但似乎只过滤当天的文件,而且时间也不正确,因为它基本上不会在定义的时间内显示文件。

对于 PowerShell,我得到了这个:

Get-ChildItem -Path c:\your\path\here -Recurse |
  Where-Object -FilterScript { $_.LastWriteTime -ge (Get-Date).AddHours(-2) }

但不确定如何修改它以获取仅包含 12am 到 6am 文件的列表。

4

2 回答 2

2

我认为这可能很有用。它检查是否LastWriteTime.Hour大于 0(上午 12 点)但小于 6(上午 6 点):

Get-ChildItem -Path c:\path -Recurse | 
    Where-Object { $_.LastWriteTime.Hour -gt 0 -and $_.LastWriteTime.Hour -lt 6}
于 2020-01-30T12:02:32.853 回答
2

尝试这个。它使用 TimeSpans 过滤午夜到早上 6 点之间的文件:

$timeAfter  = New-TimeSpan -Hours 0 -Minutes 0 -Seconds 0
$timeBefore = New-TimeSpan -Hours 6 -Minutes 0 -Seconds 0

Get-ChildItem -Path 'X:\TheFolder' -Filter '*.CCTV' -File -Recurse | 
    Where-Object { $_.LastWriteTime.TimeOfDay -gt $timeAfter -and $_.LastWriteTime.TimeOfDay -lt $timeBefore} |
    Remove-Item -WhatIf

对控制台中显示的信息感到满意后,移除-WhatIf开关

于 2020-01-30T12:15:20.013 回答