如何查看 Powershell 以查看$ fullPath中的文件是否早于“5天10小时5分钟”?
( by OLD,我是指如果它是在5天10小时5分钟之前创建或修改的话)
答案 0 :(得分:41)
这是一个非常简洁但非常易读的方法:
$lastWrite = (get-item $fullPath).LastWriteTime
$timespan = new-timespan -days 5 -hours 10 -minutes 5
if (((get-date) - $lastWrite) -gt $timespan) {
# older
} else {
# newer
}
这样做的原因是因为减去两个日期会给你一个时间跨度。时间跨度与标准运营商相当。
希望这会有所帮助。
答案 1 :(得分:6)
此powershell脚本将显示超过5天,10小时和5分钟的文件。您可以将其另存为.ps1
扩展名的文件,然后运行它:
# You may want to adjust these
$fullPath = "c:\path\to\your\files"
$numdays = 5
$numhours = 10
$nummins = 5
function ShowOldFiles($path, $days, $hours, $mins)
{
$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
if ($files -ne $NULL)
{
for ($idx = 0; $idx -lt $files.Length; $idx++)
{
$file = $files[$idx]
write-host ("Old: " + $file.Name) -Fore Red
}
}
}
ShowOldFiles $fullPath $numdays $numhours $nummins
以下是有关过滤文件的行的更多详细信息。它分为多行(可能不是合法的powershell),以便我可以包含注释:
$files = @(
# gets all children at the path, recursing into sub-folders
get-childitem $path -include *.* -recurse |
where {
# compares the mod date on the file with the current date,
# subtracting your criteria (5 days, 10 hours, 5 min)
($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins))
# only files (not folders)
-and ($_.psIsContainer -eq $false)
}
)
答案 2 :(得分:6)
Test-Path
可以为您完成此操作:
Test-Path $fullPath -OlderThan (Get-Date).AddDays(-5).AddHours(-10).AddMinutes(-5)