[英]Powershell deleting more files than it should, bug?
I'm using the following snippet of code in a powershell script to clean up files that we no longer need, however this appears to delete everything (thank God for backups...) and not just those modified older than $limit, can anyone explain this behavior? 我在Powershell脚本中使用以下代码片段来清理我们不再需要的文件,但是这似乎删除了所有内容(感谢上帝的备份...),而不仅仅是那些早于$ limit的文件,任何人都可以解释这种行为?
param (
[int]$daystokeep = 548 # default to 18 months
)
$limit = (Get-Date).AddDays(-1 * $daystokeep) # 18 months
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.ModifyTime -lt $limit } | Remove-Item -Force
FileInfo
objects have no ModifyTime
property, so your comparison basically evaluates to: FileInfo
对象没有ModifyTime
属性,因此您的比较结果基本上是:
$null -lt $limit
which is always $true
. 这始终是
$true
。
Change the property name to LastWriteTime
: 将属性名称更改为
LastWriteTime
:
$_.LastWriteTime -lt $limit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.