简体   繁体   English

按子文件夹列出文件数

[英]List file count by subfolder

I am trying to use powershell to produce a list of folder names and how many files are in each folder.我正在尝试使用 powershell 生成文件夹名称列表以及每个文件夹中有多少文件。

I have this script我有这个脚本

$dir = "C:\Users\folder" 
Get-ChildItem $dir -Recurse -Directory | ForEach-Object{
    [pscustomobject]@{
        Folder = $_.FullName
        Count = @(Get-ChildItem -Path $_.Fullname -File).Count
    }
} | Select-Object Folder,Count

Which lists the file count, but it puts the full path (ie C:\\Users\\name\\Desktop\\1\\2\\-movi... ).其中列出了文件计数,但它放置了完整路径(即C:\\Users\\name\\Desktop\\1\\2\\-movi... )。 Is there any way to just display the last folder ("movies") as well as save the result to a .txt file?有没有办法只显示最后一个文件夹(“电影”)并将结果保存到 .txt 文件中?

Thank you谢谢

Instead of $_.FullName , use $_.Name to only get the directory name .而不是$_.FullName ,使用$_.Name只获取目录名称

Your Select-Object call is redundant - it is effectively a no-op.您的Select-Object调用是多余的 - 它实际上是一个空操作。

While it's easy to send the results to a .txt file with > , for instance, it's better to use a more structured format for later programmatic processing.例如,虽然很容易将结果发送到带有>.txt文件,但最好使用更结构化的格式进行以后的编程处理。 In the simplest form, that means outputting to a CSV file via Export-Csv ;在最简单的形式中,这意味着通过Export-Csv输出到 CSV 文件; generally, however, the most faithful way of serializing objects to a file is to use Export-CliXml .然而,通常,将对象序列化为文件的最Export-CliXml是使用Export-CliXml

Using Export-Csv for serialization:使用Export-Csv进行序列化:

$dir = 'C:\Users\folder'
Get-ChildItem -LiteralPath $dir -Recurse -Directory | ForEach-Object {
    [pscustomobject] @{
      Folder = $_.Name
      Count = @(Get-ChildItem -LiteralPath $_.Fullname -File).Count
    }
} | Export-Csv -NoTypeInformation results.csv

Note that you could streamline your command by replacing the ForEach-Object call with a Select-Object call that uses a calculated property :请注意,您可以通过将ForEach-Object调用替换为使用计算属性Select-Object调用来简化命令:

$dir = 'C:\Users\folder'
Get-ChildItem -LiteralPath $dir -Recurse -Directory |
  Select-Object Name,
    @{ n='Count'; e={@(Get-ChildItem -LiteralPath $_.Fullname -File).Count} } |
      Export-Csv -NoTypeInformation results.csv

You mean something like this...你的意思是这样的......

Clear-Host
Get-ChildItem -Path 'd:\temp' -Recurse -Directory | 
Select-Object Name,FullName,
@{Name='FileCount';Expression = {(Get-ChildItem -Path $_.FullName -File -Recurse| Measure-Object).Count}} `
| Format-Table -AutoSize

# Results


Name           FullName                               FileCount
----           --------                               ---------
abcpath0       D:\temp\abcpath0                               5
abcpath1       D:\temp\abcpath1                               5
abcpath2       D:\temp\abcpath2                               5
Duplicates     D:\temp\Duplicates                         12677
EmptyFolder    D:\temp\EmptyFolder                            0
NewFiles       D:\temp\NewFiles                               4
PngFiles       D:\temp\PngFiles                               4
results        D:\temp\results                              905
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM