简体   繁体   English

如何使用Powershell过滤包含的文件数量的目录

[英]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.

相关问题 如何重命名子目录中的文件? - How do I rename files in sub directories? CMD:如何递归删除文件和目录的“隐藏”属性 - CMD: How do I recursively remove the “Hidden”-Attribute of files and directories 如何遍历目录? - How do I traverse into directories? 在C#中列出目录时如何过滤文件 - How to filter files when listing directories in C# 如何使用批处理脚本从当前目录复制文件,而不复制子目录? - How do I copy files from the current directory, but not sub-directories using a batch script? 如何删除子目录中目录的文件扩展名? - How do I remove file extension of directories in sub directories? 如何在用户的AppData目录中创建目录和文件? - How do I create directories and files within the user's AppData directory? 如何使用 Python 遍历包含子目录的目录中的文件? - How do I iterate over files in a directory including sub directories, using Python? (批处理)如何处理包含文件> inputdir> program> output.file中的100多个目录? - (batch) How do I Process 100+ directories with files inside > inputdir > program > output.file? 如何删除子目录中的文件和文件夹,但在Ruby中跳过某些文件名的删除? - How do I delete files and folders in sub-directories but skip delete for certain file names in Ruby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM