[英]How do I filter directories with powershell on the amount of files contained
I am having issues finding the correct syntax I need to filter my results on only listing directories with a file count of above a specified amount (600 in my case). 我在寻找正确的语法时遇到问题,我仅在文件数量超过指定数量(本例为600)的目录列表中才需要过滤结果。
This is my code so far; 到目前为止,这是我的代码;
$server_dir= "D:\backup"
$export_dir= "C:\support\spcount.txt"
if($server_dir)
{
$folders = Get-ChildItem $server_dir
$output = @()
foreach($folder in $folders)
{
$fname = $folder.Name
$fpath = $folder.FullName
$fcount = Get-ChildItem $fpath | Measure-Object | Select-Object -Expand Count
$obj = New-Object psobject -Property @{FolderName = $fname; FileCount = $fcount} | Format-List;
$output += $obj
}
#Output
$output | Tee-Object -FilePath $export_dir | Format-list FileCount
}
And I am getting positive results with this, it is listing all Child Items within the backup dir however I need to filter this to only display and out too text format IF the directory contains 600 or more files. 而且我得到了积极的结果,它列出了备份目录中的所有子项,但是如果目录包含600个或更多文件,我需要对其进行过滤以仅显示和显示文本格式。
Can anybody help me please? 有人可以帮我吗?
I am fairly new too powershell so please pull me up if this code is not the greatest, I am forever wanting too learn. 我还是个新手,所以如果这段代码不是最好的代码,请把我拉起来,我永远都想学习。
Thanks! 谢谢!
So in short you could do something like this to get that information. 简而言之,您可以执行以下操作来获取该信息。
Get-ChildItem C:\temp -Directory |
Select Name,@{Label="Count";Expression={(Get-Childitem $_ -file -Recurse).Count}} |
Where-Object{$_.Count -lt 10}
Let see if we can incorporate that in your code. 让我们看看是否可以将其合并到您的代码中。 Your
if
statement is also kind of pointless. 您的
if
语句也毫无意义。 Your variable contains a non-null \\ non-zerolength string so it will always be True. 您的变量包含非null \\非零长度字符串,因此它将始终为True。 You want it to work if the directory exists I imagine.
我想如果目录存在,您希望它能正常工作。
$server_dir= "D:\backup"
$export_dir= "C:\support\spcount.txt"
if(Test-Path $server_dir){
Get-ChildItem C:\temp -Directory |
Select Name,@{Label="Count";Expression={(Get-Childitem $_ -file -Recurse).Count}} |
Where-Object{$_.Count -lt 10} |
ConvertTo-Csv | Tee -File $export_dir | ConvertFrom-Csv
} Else {
Write-Warning "$server_dir does not exist."
}
Just working on getting this to file and screen with Tee
just a moment. 只需片刻就可以使用
Tee
进行归档和筛选。
I see 2 ways to do this. 我看到两种方法可以做到这一点。
Filter it in your output like this: 像这样在输出中过滤它:
$output | where -property FileCount -gt 599 | # ... your code to write to the output
Or not store it in the output array if it doesn't match the condition: 如果不符合条件,则不将其存储在输出数组中:
if ($fcount -gt 599) {
$obj = New-Object psobject -Property @{FolderName = $fname; FileCount = $fcount} | Format-List;
$output += obj
}
I think I found the issue. 我想我找到了问题。 It's that
Format-List
statement at the end of your object creation statement. 在对象创建语句的末尾就是那个
Format-List
语句。 It pipes the newly created object through Format-List
, and thus transforms it into something else. 它通过
Format-List
传递新创建的对象,从而将其转换为其他对象。
$obj = New-Object psobject -Property @{FolderName = $fname; FileCount = $fcount} | Format-List
So if you remove that last bit, you'll get the object you expect 因此,如果删除最后一点,您将获得期望的对象
$obj = New-Object psobject -Property @{FolderName = $fname; FileCount = $fcount}
So when you use the where
statement to filter, you'll actually have a FileCount property to filter on. 因此,当您使用
where
语句进行过滤时,实际上将有一个FileCount属性可以进行过滤。 I detected it by running the $output through Get-Member
which showed me it wasn't the object with the expected properties. 我通过通过
Get-Member
运行$ output来检测到它,这表明它不是具有预期属性的对象。
So basically, here's your code, including fixes: 因此,基本上,这是您的代码,包括修复程序:
if($server_dir)
{
# *** Added the -directory flag, cause we don't need those pesky files ***
$folders = Get-ChildItem $server_dir -directory
$output = @()
foreach($folder in $folders)
{
$fname = $folder.Name
$fpath = $folder.FullName
$fcount = Get-ChildItem $fpath | Measure-Object | Select-Object -Expand Count
# *** Format-List was dropped here to avoid losing the objects ***
$obj = New-Object psobject -Property @{FolderName = $fname; FileCount = $fcount}
$output += $obj
}
# *** And now the filter and we're done ***
$output | where -Property FileCount -ge 600 | Tee-Object -FilePath $export_dir | Format-list FileCount
}
Note also the -directory
to get only folders with get-childitem
, and the -ge 600
( g reater than or e qual) instead of -gt 599
which is just a bit more obvious. 还要注意的
-directory
只得到文件夹用get-childitem
和-ge 600
(G reater比或E资格赛),而不是-gt 599
这是比较明显的只是一个位。
Remember that the Format-* statements actually transform the data passed through them. 请记住,Format- *语句实际上会转换通过它们的数据。 So you should only use those at the end of the pipeline to show data on screen or dump it to a file.
因此,您仅应在管道的末尾使用这些内容,以在屏幕上显示数据或将其转储到文件中。 Don't use it to transform the data you still want to work with later on.
请勿使用它来转换以后仍要使用的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.