PowerShell总文件计数和计数文件比日期更新

发布于 2025-01-31 16:41:42 字数 505 浏览 2 评论 0 原文

我想获得比特定日期更新文件计数的总文件计数,而不必进行2个单独的呼叫。有没有办法在1个电话中获得这两个计数?

效率低下的方式:

cls
$compareDate = (Get-Date).AddDays(-365) 
$FileShare = "C:\Folder\Subfolder"
$TotalCount = (Get-ChildItem -File  -Recurse $FileShare | Measure-Object).Count
$ActiveCount = (Get-ChildItem -File -Recurse $FileShare | Where-Object { $_.LastWriteTime -gt $compareDate}).Count
$Percentage = ($ActiveCount/$TotalCount)*100
Write-Host $ActiveCount/$TotalCount " is " $Percentage.ToString("#.##") "% Active"

I want to get total file count and count of files newer than a specific date without having to do 2 separate calls. Is there a way do get both of these counts in 1 call?

Inefficient Way:

cls
$compareDate = (Get-Date).AddDays(-365) 
$FileShare = "C:\Folder\Subfolder"
$TotalCount = (Get-ChildItem -File  -Recurse $FileShare | Measure-Object).Count
$ActiveCount = (Get-ChildItem -File -Recurse $FileShare | Where-Object { $_.LastWriteTime -gt $compareDate}).Count
$Percentage = ($ActiveCount/$TotalCount)*100
Write-Host $ActiveCount/$TotalCount " is " $Percentage.ToString("#.##") "% Active"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2025-02-07 16:41:42

如果我正确理解,您只需要拨打 Get-Childitem 一次,然后根据此集合过滤以获取第二个计数( $ activeCount

$compareDate = (Get-Date).AddDays(-365)
$files = Get-ChildItem -File -Recurse "C:\Folder\SubFolder"
$totalCount  = $files.Count
$ActiveCount = $files.Where{ $_.LastWriteTime -gt $compareDate }.Count
'Thing is ' + ($ActiveCount / $totalCount).ToString('P2') + ' Active'

:还值得注意的是,由于集合( $ files )已经在内存中,因此 。where 方法 where-object> where-object cmdlet> cmdlet 用于过滤。

如果您需要的速度比.。

$ActiveCount = 0
foreach($file in $files) {
    if($file.LastWriteTime -gt $compareDate) {
        $ActiveCount++
    }
}

If I understand correctly, you only need to make the call to Get-ChildItem only once, then filter based on this collection to get the second count ($ActiveCount):

$compareDate = (Get-Date).AddDays(-365)
$files = Get-ChildItem -File -Recurse "C:\Folder\SubFolder"
$totalCount  = $files.Count
$ActiveCount = $files.Where{ $_.LastWriteTime -gt $compareDate }.Count
'Thing is ' + ($ActiveCount / $totalCount).ToString('P2') + ' Active'

It's also worth noting that, since the collection ($files) is already in memory, the .Where method is more efficient than Where-Object cmdlet for filtering.

If you need something faster than the .Where filtering technique displayed above:

$ActiveCount = 0
foreach($file in $files) {
    if($file.LastWriteTime -gt $compareDate) {
        $ActiveCount++
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文