作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在Windows 2012 R2版本上运行的服务器上
PS C:\Users\admin> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
...
...
foreach ($i in Get-ChildItem $TargetFolder -recurse -exclude
$skipFilePatterns | where { ! $i.PSIsContainer }) {
if (! $i.PSIsContainer -and $i.LastWriteTime -lt ($(Get-Date).AddDays(-$keepForDays))) {
# Add -WhatIf to test the script, remove it when confirmed
$timeStamp = $i.LastWriteTime
$fullName = $i.FullName
$log.Info("Deleting: $fullName with timestamp (LastWriteTime): $timeStamp")
Remove-Item $i.FullName -force -ErrorVariable errVar -ErrorAction SilentlyContinue
...
...
最佳答案
所以你可以使用Select -first $Limit
其次,无需排除文件夹$i.PSIsContainer
,您可以告诉Get-ChildItem
(别名GCI
)仅使用-File
参数获取文件
就像是
function Remove-ChildItemsInChunks($keepForDays, $Limit, $Path){
$count = 0
gci $Path -Recurse -File |
?{$i.LastWriteTime -lt ($(Get-Date).AddDays(-$keepForDays))} |
select -First $Limit | %{
$count += 1
Remove-Item $_
}
return $Count
}
$GCICount = Get-ChildItemsInChunks -Path C:\test -keepForDays 30 -Limit 500
while($GCICount -gt 0){
$GCICount = Get-ChildItemsInChunks -Path C:\Test -keepForDays 30 -Limit 500
}
关于powershell - 能做到吗根据文件使用Powershell的年代删除大量文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463364/
我是一名优秀的程序员,十分优秀!