如何使用 PowerShell 检查文件是否超过特定时间? [英] How can I check if a file is older than a certain time with PowerShell?

查看:58
本文介绍了如何使用 PowerShell 检查文件是否超过特定时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 Powershell 以查看 $fullPath 中的文件是否早于5 天 10 小时 5 分钟"?

How can I check in Powershell to see if a file in $fullPath is older than "5 days 10 hours 5 minutes" ?

(旧的,我的意思是如果它是在不迟于 5 天 10 小时 5 分钟的时间内创建或修改的)

(by OLD, I mean if it was created or modified NOT later than 5 days 10 hours 5 minutes)

推荐答案

这是一个相当简洁但非常易读的方法:

Here's quite a succinct yet very readable way to do this:

$lastWrite = (get-item $fullPath).LastWriteTime
$timespan = new-timespan -days 5 -hours 10 -minutes 5

if (((get-date) - $lastWrite) -gt $timespan) {
    # older
} else {
    # newer
}

之所以有效,是因为减去两个日期会给您一个时间跨度.时间跨度与标准运算符相当.

The reason this works is because subtracting two dates gives you a timespan. Timespans are comparable with standard operators.

希望这会有所帮助.

这篇关于如何使用 PowerShell 检查文件是否超过特定时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆