如何使用 Powershell 计算文件夹和子文件夹中的文件

How can I count files in folder and subfolder using Powershell

我正在尝试计算子文件夹中的所有文件并提取到 csv。这是文件结构。

C:\TEST\FolderAAA

  |-folderA1
     |-folderA1.1
        |-file
        |-file
     |-folderA1.2
        |-file
        |-file
     |-folderA1.3
        |-file
        |-file

   |-folderA2
     |-folderA1.1
        |-file
        |-file
     |-folderA2.2
        |-file
        |-file
     |-folderA2.3
        |-file
        |-file

C:\TEST\FolderBBB

  |-folderB1
     |-folderB1.1
        |-file
        |-file
     |-folderB1.2
        |-file
        |-file
     |-folderB1.3
        |-file
        |-file

   |-folderB2
     |-folderB2.1
        |-file
        |-file
     |-folderB2.2
        |-file
        |-file
     |-folderB3.3
        |-file
        |-file

C:\TEST\FolderCCC

  |-folderC1
     |-folderC1.1
        |-file
        |-file
     |-folderC1.2
        |-file
        |-file
     |-folderC1.3
        |-file
        |-file

   |-folderC2
     |-folderC2.1
        |-file
        |-file
     |-folderC2.2
        |-file
        |-file
     |-folderC3.3
        |-file
        |-file

我正在寻找具有以下格式的 CSV 输出:

Column A          Column B
Folders           File Count
----------------------------
FolderAAA                   0
folderA1                    0
folderA1.1                  2
folderA1.2                  2
folderA1.3                  2
folderA2                    0
folderA2.1                  2
folderA2.2                  2
folderA2.3                  2

Space/New Blank Row --------

FolderBBB                   0
folderB1                    0
folderB1.1                  2
folderB1.2                  2
folderB1.3                  2
folderB2                    0
folderB2.1                  2
folderB2.2                  2
folderB2.3                  2

Space/New Blank Row --------

FolderCCC                   0
folderC1                    0
folderC1.1                  2
folderC1.2                  2
folderC1.3                  2
folderC2                    0
folderC2.1                  2
folderC2.2                  2
folderC2.3                  2

我在这里做了一些研究后尝试并修改了这段代码:

$FOLDER_ROOT = "C:\TEST\"
$OUTPUT_LOCATION = "C:\TEST\RESULT\Folder_Count.txt"
$OUTPUT_CSV = "C:\TEST\RESULT\Folder_Count.csv"

function DirX($directory) {
    Remove-Item $OUTPUT_LOCATION

    foreach ($singleDirectory in (Get-ChildItem $directory -Recurse -Directory)) {
        $count = Get-ChildItem $singleDirectory.FullName -File |
                 Measure-Object |
                 %{$_.Count}
        $summary = $singleDirectory.Basename + " - " + $count
        Add-Content $OUTPUT_LOCATION $summary
    }
}
DirX($FOLDER_ROOT)

Import-Csv $OUTPUT_LOCATION -Delimiter "-" -Header Folder, Count |
    Export-Csv $OUTPUT_CSV -NoTypeInformation

我得到的摘录没有按照我想要实现的格式进行格式化。

目前摘录的格式不正确:

Column A          Column B
Folders           File Count
----------------------------
FolderAAA                  0
folderA1                   0
folderA2                   0
folderA1.1                 2
folderA1.2                 2
folderA1.3                 2
folderA2.1                 2
folderA2.2                 2
folderA2.3                 2

为什么不先收集数据,然后排序输出?

$FOLDER_ROOT = "C:\TEST"
$OUTPUT_CSV = "C:\TEST\Folder_Count.csv"

Function DirX($directory) {
    Write-Host "Working..." -NoNewline
    $Data = Get-ChildItem $directory -Recurse -Directory | ForEach-Object {
        Write-Host "."-NoNewline
        $count = (Get-ChildItem $_.FullName -File).count #  | Measure-Object | %{$_.Count}
        [pscustomobject]@{FilePath = $_.fullName;Filecount = $count}
    }
    Write-Host "Done"
    $Data | Sort-Object Filepath
}


$ThisPath = Dirx $FOLDER_ROOT
$ThisPath | Export-Csv -Path $OUTPUT_CSV -NoTypeInformation

我同意 ,但建议将其简化为管道,如下所示:

Get-ChildItem $FOLDER_ROOT -Recurse -Directory |
    Sort-Object FullName |
    Select-Object FullName, @{n='Count';e={(Get-ChildItem $_.FullName -File).Count}} |
    Export-Csv -Path $OUTPUT_CSV -NoType